id
stringlengths
3
12
title
stringlengths
3
66
title_slug
stringlengths
3
66
description
stringlengths
39
25.4k
description_md
stringlengths
39
4.82k
difficulty
stringclasses
113 values
tags
listlengths
0
9
source
stringclasses
5 values
url
stringlengths
37
96
type
stringclasses
2 values
release_timestamp
int64
1.7B
1.73B
release_date
stringlengths
19
19
time_limit_nanos
int64
1B
9B
memory_limit_bytes
int64
256M
2.1B
starter_code
dict
solutions
dict
test_case_generator
stringlengths
521
16.9k
evaluator
stringlengths
200
5.31k
generated_tests
stringlengths
3.2k
359M
test_runners
dict
3691
Minimum Operations to Make Columns Strictly Increasing
minimum-operations-to-make-columns-strictly-increasing
<p>You are given a <code>m x n</code> matrix <code>grid</code> consisting of <b>non-negative</b> integers.</p> <p>In one operation, you can increment the value of any <code>grid[i][j]</code> by 1.</p> <p>Return the <strong>minimum</strong> number of operations needed to make all columns of <code>grid</code> <strong>s...
You are given a `m x n` matrix `grid` consisting of **non\-negative** integers. In one operation, you can increment the value of any `grid[i][j]` by 1\. Return the **minimum** number of operations needed to make all columns of `grid` **strictly increasing**. **Example 1:** **Input:** grid \= \[\[3,2],\[1,3...
Easy
[ "array", "greedy", "matrix" ]
leetcode
https://leetcode.com/problems/minimum-operations-to-make-columns-strictly-increasing
functional
null
null
null
null
{ "c": "int minimumOperations(int** grid, int gridSize, int* gridColSize) {\n \n}", "cpp": "class Solution {\npublic:\n int minimumOperations(vector<vector<int>>& grid) {\n \n }\n};", "csharp": "public class Solution {\n public int MinimumOperations(int[][] grid) {\n \n }\n}", "dart...
{ "cpp": { "code": "class Solution {\npublic:\n int minimumOperations(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n long long total = 0;\n for (int j = 0; j < n; j++) {\n int cur = grid[0][j];\n for (int i = 1; i < m; i++) {\n ...
def generate_test_cases(num_cases: int, seed: int = 42) -> list[dict]: import random random.seed(seed) def sample_solution(grid: list[list[int]]) -> int: m = len(grid) n = len(grid[0]) operations = 0 for j in range(n): prev = grid[0][j] for i in range...
def evaluate(expected_output: str, program_output: str) -> bool: try: # Deserialization: Our output is a single integer represented as a string. expected = int(expected_output.strip()) program = int(program_output.strip()) return expected == program except Exception: retu...
[{"input": "1 1\n0", "output": "0"}, {"input": "1 5\n1,5,3,2,8", "output": "0"}, {"input": "5 1\n1\n1\n1\n1\n1", "output": "10"}, {"input": "3 2\n0,0\n1,1\n2,2", "output": "0"}, {"input": "4 2\n3,2\n1,3\n3,4\n0,1", "output": "15"}, {"input": "3 3\n3,2,1\n2,1,0\n1,2,3", "output": "12"}, {"input": "1 5\n1003,914,571,419,...
{ "cpp": "==Code Submission==\n\nvector<vector<int>> deserialize_stdin(const string& input) {\n vector<vector<int>> grid;\n istringstream iss(input);\n string firstLine;\n getline(iss, firstLine);\n int m, n;\n {\n istringstream fl(firstLine);\n fl >> m >> n;\n }\n for (int i = 0...
3696
Count Substrings Divisible By Last Digit
count-substrings-divisible-by-last-digit
<p>You are given a string <code>s</code> consisting of digits.</p> <p>Return the <strong>number</strong> of <span data-keyword="substring-nonempty">substrings</span> of <code>s</code> <strong>divisible</strong> by their <strong>non-zero</strong> last digit.</p> <p><strong>Note</strong>: A substring may contain leadin...
You are given a string `s` consisting of digits. Return the **number** of substrings of `s` **divisible** by their **non\-zero** last digit. **Note**: A substring may contain leading zeros. **Example 1:** **Input:** s \= "12936" **Output:** 11 **Explanation:** Substrings `"29"`, `"129"`, `"293"` and ...
Hard
[ "string", "dynamic-programming" ]
leetcode
https://leetcode.com/problems/count-substrings-divisible-by-last-digit
functional
null
null
null
null
{ "c": "long long countSubstrings(char* s) {\n \n}", "cpp": "class Solution {\npublic:\n long long countSubstrings(string s) {\n \n }\n};", "csharp": "public class Solution {\n public long CountSubstrings(string s) {\n \n }\n}", "dart": "class Solution {\n int countSubstrings(Strin...
{ "cpp": { "code": "class Solution {\npublic:\n long long countSubstrings(string s) {\n int n = s.size();\n long long ans = 0;\n vector<long long> freq3(3,0), freq9(9,0), freq7(7,0);\n freq3[0] = 1;\n freq9[0] = 1;\n freq7[0] = 1;\n int cur3 = 0, cur9 = 0;\n ...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random random.seed(seed) # Sample solution based on the provided implementation. def sample_solution(s: str) -> int: n = len(s) ans = 0 freq3 = {0: 1} p3 = 0 freq9 = {0: 1} p9 = 0...
def evaluate(expected_output: str, program_output: str) -> bool: # Use the same deserialization: strip whitespace and treat outputs as integers. try: expected = int(expected_output.strip()) result = int(program_output.strip()) return expected == result except Exception: retur...
[{"input": "12936", "output": "11"}, {"input": "5701283", "output": "18"}, {"input": "1010101010", "output": "25"}, {"input": "1", "output": "1"}, {"input": "0", "output": "0"}, {"input": "11111", "output": "15"}, {"input": "99999", "output": "15"}, {"input": "404", "output": "4"}, {"input": "248", "output": "6"}, {"in...
{ "cpp": "==Code Submission==\n\nstring deserialize_stdin() {\n string input;\n getline(cin, input);\n return input;\n}\n\nstring serialize_stdout(long long result) {\n return to_string(result);\n}\n\nint main() {\n string s = deserialize_stdin();\n Solution sol; // Submitted solution will be inject...
3702
Maximum Subarray With Equal Products
maximum-subarray-with-equal-products
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>An array <code>arr</code> is called <strong>product equivalent</strong> if <code>prod(arr) == lcm(arr) * gcd(arr)</code>, where:</p> <ul> <li><code>prod(arr)</code> is the product of all elements of <code>arr</code>.</li> <li>...
You are given an array of **positive** integers `nums`. An array `arr` is called **product equivalent** if `prod(arr) == lcm(arr) * gcd(arr)`, where: - `prod(arr)` is the product of all elements of `arr`. - `gcd(arr)` is the GCD of all elements of `arr`. - `lcm(arr)` is the LCM of all elements of `arr`. Return th...
Easy
[ "array", "math", "sliding-window", "enumeration", "number-theory" ]
leetcode
https://leetcode.com/problems/maximum-subarray-with-equal-products
functional
null
null
null
null
{ "c": "int maxLength(int* nums, int numsSize) {\n \n}", "cpp": "class Solution {\npublic:\n int maxLength(vector<int>& nums) {\n \n }\n};", "csharp": "public class Solution {\n public int MaxLength(int[] nums) {\n \n }\n}", "dart": "class Solution {\n int maxLength(List<int> nums)...
{ "cpp": { "code": "class Solution {\npublic:\n int maxLength(vector<int>& nums) {\n int n = nums.size(), ans = 0;\n auto gcdFun = [&](int a, int b) {\n while(b) { int t = a % b; a = b; b = t; }\n return a;\n };\n vector<vector<int>> pe(11, vector<int>(4,0));\n...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random random.seed(seed) # Helper function for computing gcd def gcd(a, b): while b: a, b = b, a % b return a # Sample solution function adapted from the provided sample solution. # It computes ...
def evaluate(expected_output: str, program_output: str) -> bool: """ Compares the expected output with the program output. Both outputs are deserialized using simple whitespace trimming and then converted to integers. """ try: exp_val = int(expected_output.strip()) prog_val = int(pro...
[{"input": "1,1", "output": "2"}, {"input": "1,2,1,2,1,1,1", "output": "5"}, {"input": "2,3,4,5,6", "output": "3"}, {"input": "1,2,3,1,4,5,1", "output": "5"}, {"input": "10,10,10", "output": "2"}, {"input": "2,1,5,4,4,3,2,9,2,10,7,1,1,2,4,4,9,10,1,9,4,9,7,4,8,10,5,1,3,7,6,5,3,4,6,2,2,7,2,6,6,10,5,1,8,9,2,7,2,9,5,10,6,1...
{ "cpp": "==Code Submission==\n\nvector<int> deserialize_stdin(const string &input) {\n vector<int> nums;\n stringstream ss(input);\n string token;\n while(getline(ss, token, ',')) {\n if (!token.empty())\n nums.push_back(stoi(token));\n }\n return nums;\n}\n\nstring serialize_stdo...
3704
Count Partitions with Even Sum Difference
count-partitions-with-even-sum-difference
<p>You are given an integer array <code>nums</code> of length <code>n</code>.</p> <p>A <strong>partition</strong> is defined as an index <code>i</code> where <code>0 &lt;= i &lt; n - 1</code>, splitting the array into two <strong>non-empty</strong> subarrays such that:</p> <ul> <li>Left subarray contains indices <co...
You are given an integer array `nums` of length `n`. A **partition** is defined as an index `i` where `0 <= i < n - 1`, splitting the array into two **non\-empty** subarrays such that: - Left subarray contains indices `[0, i]`. - Right subarray contains indices `[i + 1, n - 1]`. Return the number of **partitions*...
Easy
[ "array", "math", "prefix-sum" ]
leetcode
https://leetcode.com/problems/count-partitions-with-even-sum-difference
functional
null
null
null
null
{ "c": "int countPartitions(int* nums, int numsSize) {\n \n}", "cpp": "class Solution {\npublic:\n int countPartitions(vector<int>& nums) {\n \n }\n};", "csharp": "public class Solution {\n public int CountPartitions(int[] nums) {\n \n }\n}", "dart": "class Solution {\n int countPa...
{ "cpp": { "code": "class Solution {\npublic:\n int countPartitions(vector<int>& nums) {\n int total = 0;\n for (int num : nums) {\n total += num;\n }\n if (total % 2 != 0) {\n return 0;\n }\n return nums.size() - 1;\n }\n};", "memory": 220...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random random.seed(seed) # Sample solution based on the provided sample: def sample_solution(nums): if sum(nums) % 2 != 0: return 0 return len(nums) - 1 test_cases = [] categories = [] # Bo...
def evaluate(expected_output: str, program_output: str) -> bool: try: # The serialization format is a simple integer in string form. expected = int(expected_output.strip()) program = int(program_output.strip()) return expected == program except Exception: return False
[{"input": "1,1", "output": "1"}, {"input": "1,2", "output": "0"}, {"input": "2,4", "output": "1"}, {"input": "2,4,6,8", "output": "3"}, {"input": "1,3,5,7", "output": "3"}, {"input": "10,10,3,7,6", "output": "4"}, {"input": "1,2,2", "output": "0"}, {"input": "4,14,16,18,30,32,36,82,96,96", "output": "9"}, {"input": "9...
{ "cpp": "==Code Submission==\nvector<int> deserialize_stdin(const string &input) {\n vector<int> nums;\n string token;\n stringstream ss(input);\n while(getline(ss, token, ',')) {\n if (!token.empty())\n nums.push_back(stoi(token));\n }\n return nums;\n}\n\nstring serialize_stdout...
3708
Zigzag Grid Traversal With Skip
zigzag-grid-traversal-with-skip
<p>You are given an <code>m x n</code> 2D array <code>grid</code> of <strong>positive</strong> integers.</p> <p>Your task is to traverse <code>grid</code> in a <strong>zigzag</strong> pattern while skipping every <strong>alternate</strong> cell.</p> <p>Zigzag pattern traversal is defined as following the below action...
You are given an `m x n` 2D array `grid` of **positive** integers. Your task is to traverse `grid` in a **zigzag** pattern while skipping every **alternate** cell. Zigzag pattern traversal is defined as following the below actions: - Start at the top\-left cell `(0, 0)`. - Move *right* within a row until the end ...
Easy
[ "array", "matrix", "simulation" ]
leetcode
https://leetcode.com/problems/zigzag-grid-traversal-with-skip
functional
null
null
null
null
{ "c": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* zigzagTraversal(int** grid, int gridSize, int* gridColSize, int* returnSize) {\n \n}", "cpp": "class Solution {\npublic:\n vector<int> zigzagTraversal(vector<vector<int>>& grid) {\n \n }\n};", "csharp...
{ "cpp": { "code": "class Solution {\npublic:\n vector<int> zigzagTraversal(vector<vector<int>>& grid) {\n vector<int> result;\n bool take = true;\n for (int i = 0; i < grid.size(); i++) {\n if (i % 2 == 0) {\n for (int j = 0; j < grid[i].size(); j++) {\n ...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random random.seed(seed) # Helper: serialize a 2D grid into a string. # Rows are separated by ";" and numbers within a row by "," def serialize_grid(grid): return ";".join(",".join(str(num) for num in row) for row i...
def evaluate(expected_output: str, program_output: str) -> bool: # Helper to deserialize output: comma-separated integers. def deserialize_output(s): s = s.strip() if s == "": return [] return list(map(int, s.split(","))) expected = deserialize_output(expected_output...
[{"input": "1,2;3,4", "output": "1,4"}, {"input": "2,1;2,1;2,1", "output": "2,1,2"}, {"input": "1,2,3;4,5,6;7,8,9", "output": "1,3,5,7,9"}, {"input": "1,1;1,1", "output": "1,1"}, {"input": "1,2,3,4;5,6,7,8", "output": "1,3,8,6"}, {"input": "10,20,30;40,50,60;70,80,90;100,110,120", "output": "10,30,50,70,90,110"}, {"inp...
{ "cpp": "==Code Submission==\n\nvector<vector<int>> deserialize_stdin(const string &input) {\n vector<vector<int>> grid;\n size_t start = 0;\n while (start < input.size()) {\n size_t end = input.find(';', start);\n string rowStr;\n if (end == string::npos) {\n rowStr = input....
3709
Find Special Substring of Length K
find-special-substring-of-length-k
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>Determine if there exists a <span data-keyword="substring-nonempty">substring</span> of length <strong>exactly</strong> <code>k</code> in <code>s</code> that satisfies the following conditions:</p> <ol> <li>The substring consists of <stro...
You are given a string `s` and an integer `k`. Determine if there exists a substring of length **exactly** `k` in `s` that satisfies the following conditions: 1. The substring consists of **only one distinct character** (e.g., `"aaa"` or `"bbb"`). 2. If there is a character **immediately before** the substring, it ...
Easy
[ "string" ]
leetcode
https://leetcode.com/problems/find-special-substring-of-length-k
functional
null
null
null
null
{ "c": "bool hasSpecialSubstring(char* s, int k) {\n \n}", "cpp": "class Solution {\npublic:\n bool hasSpecialSubstring(string s, int k) {\n \n }\n};", "csharp": "public class Solution {\n public bool HasSpecialSubstring(string s, int k) {\n \n }\n}", "dart": "class Solution {\n bo...
{ "cpp": { "code": "class Solution {\npublic:\n bool hasSpecialSubstring(string s, int k) {\n int n = s.size();\n for (int i = 0; i <= n - k; i++) {\n bool allSame = true;\n for (int j = i; j < i + k; j++) {\n if (s[j] != s[i]) {\n allSame =...
def generate_test_cases(num_cases: int, seed: int = 42) -> list[dict]: import random random.seed(seed) # Sample solution based on the provided sample solution. def sample_solution(s: str, k: int) -> bool: n = len(s) for i in range(n - k + 1): if s[i:i+k] == s[i] * k: ...
def evaluate(expected_output: str, program_output: str) -> bool: # Deserialization: Trim and lower-case the outputs. def deserialize_bool(output_str: str) -> bool: normalized = output_str.strip().lower() if normalized == "true": return True elif normalized == "false": ...
[{"input": "aaabaaa 3", "output": "true"}, {"input": "abc 2", "output": "false"}, {"input": "aaa 3", "output": "true"}, {"input": "aaaaa 3", "output": "false"}, {"input": "aaab 3", "output": "true"}, {"input": "baaa 3", "output": "true"}, {"input": "abbbacd 3", "output": "true"}, {"input": "abbaaaacc 4", "output": "tru...
{ "cpp": "==Code Submission==\n\npair<string, int> deserialize_stdin(const string &input) {\n istringstream iss(input);\n string s;\n int k;\n iss >> s >> k;\n return {s, k};\n}\n\nstring serialize_stdout(bool result) {\n return result ? \"true\" : \"false\";\n}\n\nint main(){\n ios::sync_with_st...
3712
Minimum Cost to Make Arrays Identical
minimum-cost-to-make-arrays-identical
<p>You are given two integer arrays <code>arr</code> and <code>brr</code> of length <code>n</code>, and an integer <code>k</code>. You can perform the following operations on <code>arr</code> <em>any</em> number of times:</p> <ul> <li>Split <code>arr</code> into <em>any</em> number of <strong>contiguous</strong> <spa...
You are given two integer arrays `arr` and `brr` of length `n`, and an integer `k`. You can perform the following operations on `arr` *any* number of times: - Split `arr` into *any* number of **contiguous** subarrays and rearrange these subarrays in *any order*. This operation has a fixed cost of `k`. - Choose any el...
Medium
[ "array", "greedy", "sorting" ]
leetcode
https://leetcode.com/problems/minimum-cost-to-make-arrays-identical
functional
null
null
null
null
{ "c": "long long minCost(int* arr, int arrSize, int* brr, int brrSize, long long k) {\n \n}", "cpp": "class Solution {\npublic:\n long long minCost(vector<int>& arr, vector<int>& brr, long long k) {\n \n }\n};", "csharp": "public class Solution {\n public long MinCost(int[] arr, int[] brr, lon...
{ "cpp": { "code": "class Solution {\npublic:\n long long minCost(vector<int>& arr, vector<int>& brr, long long k) {\n int n = arr.size();\n long long costNoReorder = 0;\n for (int i = 0; i < n; i++) {\n costNoReorder += llabs((long long)arr[i] - (long long)brr[i]);\n }\n...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random random.seed(seed) def sample_solution(arr, brr, k): # Sample solution as provided in the prompt. direct_cost = sum(abs(a - b) for a, b in zip(arr, brr)) sorted_arr = sorted(arr) sorted_brr = s...
def evaluate(expected_output: str, program_output: str) -> bool: # Deserialization: both expected and program outputs are serialized as a plain integer. def deserialize(output_str: str) -> int: return int(output_str.strip()) try: expected = deserialize(expected_output) program = dese...
[{"input": "-7,9,5|7,-2,-5|2", "output": "13"}, {"input": "2,1|2,1|0", "output": "0"}, {"input": "-94,89|-95,86|28", "output": "4"}, {"input": "88,-74,73|88,-74,73|94", "output": "0"}, {"input": "-78,51,8,-92,-93,-77,-45,-41,29|54,-94,43,-50,83,66,79,39,7|28", "output": "595"}, {"input": "50,-29,-99,94,-60,78,8,-13|49,...
{ "cpp": "==Code Submission==\n\nint main() {\n string input;\n getline(cin, input);\n // Expected format: \"arr|brr|k\" where arr and brr are comma-separated integers.\n size_t pos1 = input.find('|');\n size_t pos2 = input.find('|', pos1 + 1);\n string arr_str = input.substr(0, pos1);\n string b...
3721
Count Mentions Per User
count-mentions-per-user
<p>You are given an integer <code>numberOfUsers</code> representing the total number of users and an array <code>events</code> of size <code>n x 3</code>.</p> <p>Each <code inline="">events[i]</code> can be either of the following two types:</p> <ol> <li><strong>Message Event:</strong> <code>[&quot;MESSAGE&quot;, &q...
You are given an integer `numberOfUsers` representing the total number of users and an array `events` of size `n x 3`. Each `events[i]` can be either of the following two types: 1. **Message Event:** `["MESSAGE", "timestampi", "mentions_stringi"]` - This event indicates that a set of users was mentioned in a messa...
Medium
[ "array", "math", "sorting", "simulation" ]
leetcode
https://leetcode.com/problems/count-mentions-per-user
functional
null
null
null
null
{ "c": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* countMentions(int numberOfUsers, char*** events, int eventsSize, int* eventsColSize, int* returnSize) {\n \n}", "cpp": "class Solution {\npublic:\n vector<int> countMentions(int numberOfUsers, vector<vector<strin...
{ "cpp": { "code": "class Solution {\npublic:\n vector<int> countMentions(int numberOfUsers, vector<vector<string>>& events) {\n vector<tuple<int, int, int, vector<string>>> sorted_events;\n for (int i = 0; i < events.size(); ++i) {\n int timestamp = stoi(events[i][1]);\n in...
def generate_test_cases(num_cases: int, seed: int = 42) -> list[dict]: import random random.seed(seed) # Sample solution logic adapted from the provided sample solution. def sample_count_mentions(numberOfUsers, events): mentions = [0] * numberOfUsers offline_until = [0] * numberOfUs...
def evaluate(expected_output: str, program_output: str) -> bool: # Deserialization: our output format is a comma-separated list of integers. def deserialize(output_str): # Remove extra whitespace and newline characters. tokens = [token for token in output_str.strip().split(",") if token != ""] ...
[{"input": "2\n3\nMESSAGE|10|id1 id0\nOFFLINE|11|0\nMESSAGE|71|HERE", "output": "2,2"}, {"input": "2\n3\nMESSAGE|10|id1 id0\nOFFLINE|11|0\nMESSAGE|12|ALL", "output": "2,2"}, {"input": "2\n2\nOFFLINE|10|0\nMESSAGE|12|HERE", "output": "0,1"}, {"input": "3\n1\nMESSAGE|5|id0 id0 id1 ALL", "output": "3,2,1"}, {"input": "3\n...
{ "cpp": "==Code Submission==\n\npair<int, vector<vector<string>>> deserialize_stdin() {\n int numberOfUsers, n;\n string line;\n getline(cin, line);\n numberOfUsers = stoi(line);\n getline(cin, line);\n n = stoi(line);\n vector<vector<string>> events;\n for (int i = 0; i < n; i++) {\n ...
3723
Sum of Good Numbers
sum-of-good-numbers
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>, an element <code>nums[i]</code> is considered <strong>good</strong> if it is <strong>strictly</strong> greater than the elements at indices <code>i - k</code> and <code>i + k</code> (if those indices exist). If neither of these indices <em>e...
Given an array of integers `nums` and an integer `k`, an element `nums[i]` is considered **good** if it is **strictly** greater than the elements at indices `i - k` and `i + k` (if those indices exist). If neither of these indices *exists*, `nums[i]` is still considered **good**. Return the **sum** of all the **good*...
Easy
[ "array" ]
leetcode
https://leetcode.com/problems/sum-of-good-numbers
functional
null
null
null
null
{ "c": "int sumOfGoodNumbers(int* nums, int numsSize, int k) {\n \n}", "cpp": "class Solution {\npublic:\n int sumOfGoodNumbers(vector<int>& nums, int k) {\n \n }\n};", "csharp": "public class Solution {\n public int SumOfGoodNumbers(int[] nums, int k) {\n \n }\n}", "dart": "class S...
{ "cpp": { "code": "class Solution {\npublic:\n int sumOfGoodNumbers(vector<int>& nums, int k) {\n int sum = 0;\n int n = nums.size();\n for (int i = 0; i < n; ++i) {\n bool good = true;\n int left = i - k;\n if (left >= 0 && nums[i] <= nums[left]) {\n ...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random random.seed(seed) def sample_solution(nums, k): total = 0 n = len(nums) for i in range(n): left = i - k right = i + k left_ok = left < 0 or nums[i] > nums[left] ...
def evaluate(expected_output: str, program_output: str) -> bool: try: expected_val = int(expected_output.strip()) prog_val = int(program_output.strip()) return expected_val == prog_val except ValueError: return expected_output.strip() == program_output.strip()
[{"input": "2,1 1", "output": "2"}, {"input": "1,3,2,1,5,4 2", "output": "12"}, {"input": "5,5,5,5 1", "output": "0"}, {"input": "9,8,7,6,5,4 2", "output": "17"}, {"input": "1,2,3,4,5,6 2", "output": "11"}, {"input": "3,1,4,1,5 2", "output": "5"}, {"input": "115,26,760,282,251,229,143,755,105,693,759,914,559,90,605,433...
{ "cpp": "==Code Submission==\n\npair<vector<int>, int> deserialize_stdin(const string& input) {\n size_t spacePos = input.find(' ');\n string nums_str = input.substr(0, spacePos);\n vector<int> nums;\n size_t start = 0, end = 0;\n while ((end = nums_str.find(',', start)) != string::npos) {\n nu...
3731
Sum of Variable Length Subarrays
sum-of-variable-length-subarrays
<p>You are given an integer array <code>nums</code> of size <code>n</code>. For <strong>each</strong> index <code>i</code> where <code>0 &lt;= i &lt; n</code>, define a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[start ... i]</code> where <code>start = max(0, i - nums[i])</code>.</p> <p>Return th...
You are given an integer array `nums` of size `n`. For **each** index `i` where `0 <= i < n`, define a subarray `nums[start ... i]` where `start = max(0, i - nums[i])`. Return the total sum of all elements from the subarray defined for each index in the array. **Example 1:** **Input:** nums \= \[2,3,1] **O...
Easy
[ "array", "prefix-sum" ]
leetcode
https://leetcode.com/problems/sum-of-variable-length-subarrays
functional
null
null
null
null
{ "c": "int subarraySum(int* nums, int numsSize) {\n \n}", "cpp": "class Solution {\npublic:\n int subarraySum(vector<int>& nums) {\n \n }\n};", "csharp": "public class Solution {\n public int SubarraySum(int[] nums) {\n \n }\n}", "dart": "class Solution {\n int subarraySum(List<in...
{ "cpp": { "code": "class Solution {\npublic:\n int subarraySum(vector<int>& nums) {\n int total = 0;\n int n = nums.size();\n for (int i = 0; i < n; ++i) {\n int start = std::max(0, i - nums[i]);\n for (int j = start; j <= i; ++j) {\n total += nums[j];...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random # Sample solution based on the provided sample solution. def sample_solution(nums): total = 0 n = len(nums) for i in range(n): start = max(0, i - nums[i]) for j in range(start, i +...
def evaluate(expected_output: str, program_output: str) -> bool: try: expected = int(expected_output.strip()) actual = int(program_output.strip()) return expected == actual except Exception: return False
[{"input": "1", "output": "1"}, {"input": "2,3,1", "output": "11"}, {"input": "3,1,1,2", "output": "13"}, {"input": "1,1,1,1", "output": "7"}, {"input": "5,4,3,2,1", "output": "38"}, {"input": "100,999,1,50", "output": "3349"}, {"input": "1,2,3,4,5,6,7,8,9,10", "output": "220"}, {"input": "115,26,760,282,251,229,143,75...
{ "cpp": "==Code Submission==\n\nvector<int> deserialize_stdin(const string &input) {\n vector<int> nums;\n stringstream ss(input);\n string token;\n while(getline(ss, token, ',')) {\n if(!token.empty()) {\n nums.push_back(stoi(token));\n }\n }\n return nums;\n}\n\nstring se...
3733
Length of Longest V-Shaped Diagonal Segment
length-of-longest-v-shaped-diagonal-segment
<p>You are given a 2D integer matrix <code>grid</code> of size <code>n x m</code>, where each element is either <code>0</code>, <code>1</code>, or <code>2</code>.</p> <p>A <strong>V-shaped diagonal segment</strong> is defined as:</p> <ul> <li>The segment starts with <code>1</code>.</li> <li>The subsequent elements ...
You are given a 2D integer matrix `grid` of size `n x m`, where each element is either `0`, `1`, or `2`. A **V\-shaped diagonal segment** is defined as: - The segment starts with `1`. - The subsequent elements follow this infinite sequence: `2, 0, 2, 0, ...`. - The segment: - Starts **along** a diagonal direction ...
Hard
[ "array", "dynamic-programming", "memoization", "matrix" ]
leetcode
https://leetcode.com/problems/length-of-longest-v-shaped-diagonal-segment
functional
null
null
null
null
{ "c": "int lenOfVDiagonal(int** grid, int gridSize, int* gridColSize) {\n \n}", "cpp": "class Solution {\npublic:\n int lenOfVDiagonal(vector<vector<int>>& grid) {\n \n }\n};", "csharp": "public class Solution {\n public int LenOfVDiagonal(int[][] grid) {\n \n }\n}", "dart": "class...
{ "cpp": { "code": "class Solution {\npublic:\n int lenOfVDiagonal(vector<vector<int>>& grid) {\n int n = grid.size();\n if (n == 0) return 0;\n int m = grid[0].size();\n if (m == 0) return 0;\n\n vector<vector<vector<int>>> len(4, vector<vector<int>>(n, vector<int>(m, 0)));\...
import random def generate_test_cases(num_cases: int, seed: int = 42) -> list: random.seed(seed) # Helper: Sample solution adapted from the provided solution. def solve(grid): n = len(grid) if n == 0: return 0 m = len(grid[0]) if m == 0: return 0...
def evaluate(expected_output: str, program_output: str) -> bool: """ Compares the expected output to the program output. Both are serialized as simple strings. Here, the output is an integer. """ try: expected_val = int(expected_output.strip()) program_val = int(program_output.strip(...
[{"input": "2,2,1,2,2;2,0,2,2,0;2,0,1,1,0;1,0,2,2,2;2,0,0,2,2", "output": "5"}, {"input": "2,2,2,2,2;2,0,2,2,0;2,0,1,1,0;1,0,2,2,2;2,0,0,2,2", "output": "4"}, {"input": "1,2,2,2,2;2,2,2,2,0;2,0,0,0,0;0,0,2,2,2;2,0,0,2,0", "output": "5"}, {"input": "1", "output": "1"}, {"input": "0,0,0,2,0", "output": "0"}, {"input": "2...
{ "cpp": "==Code Submission==\n\nvector<vector<int>> deserialize_stdin(const string &input) {\n vector<vector<int>> grid;\n stringstream ss(input);\n string row;\n while(getline(ss, row, ';')) {\n vector<int> nums;\n stringstream rowStream(row);\n string token;\n while(getline(...
3736
Find Valid Pair of Adjacent Digits in String
find-valid-pair-of-adjacent-digits-in-string
<p>You are given a string <code>s</code> consisting only of digits. A <strong>valid pair</strong> is defined as two <strong>adjacent</strong> digits in <code>s</code> such that:</p> <ul> <li>The first digit is <strong>not equal</strong> to the second.</li> <li>Each digit in the pair appears in <code>s</code> <strong...
You are given a string `s` consisting only of digits. A **valid pair** is defined as two **adjacent** digits in `s` such that: - The first digit is **not equal** to the second. - Each digit in the pair appears in `s` **exactly** as many times as its numeric value. Return the first **valid pair** found in the string...
Easy
[ "hash-table", "string", "counting" ]
leetcode
https://leetcode.com/problems/find-valid-pair-of-adjacent-digits-in-string
functional
null
null
null
null
{ "c": "char* findValidPair(char* s) {\n \n}", "cpp": "class Solution {\npublic:\n string findValidPair(string s) {\n \n }\n};", "csharp": "public class Solution {\n public string FindValidPair(string s) {\n \n }\n}", "dart": "class Solution {\n String findValidPair(String s) {\n ...
{ "cpp": { "code": "class Solution {\npublic:\n string findValidPair(string s) {\n int count[10] = {0};\n for (char c : s) {\n count[c - '0']++;\n }\n for (int i = 0; i < s.size() - 1; ++i) {\n char a = s[i], b = s[i+1];\n if (a != b) {\n ...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random random.seed(seed) def sample_solution(s: str) -> str: # This is the sample solution from the problem description. freq = {} for c in s: freq[c] = freq.get(c, 0) + 1 for i in range(...
def evaluate(expected_output: str, program_output: str) -> bool: # Use simple deserialization: remove leading/trailing whitespace. expected = expected_output.strip() program = program_output.strip() return expected == program
[{"input": "2523533", "output": "23"}, {"input": "221", "output": "21"}, {"input": "22", "output": ""}, {"input": "444455555", "output": "45"}, {"input": "312233", "output": "31"}, {"input": "111", "output": ""}, {"input": "121", "output": ""}, {"input": "2154432927112449194974851376534622726651892729564214524275863664...
{ "cpp": "==Code Submission==\n\nstring deserialize_stdin() {\n string input;\n getline(cin, input);\n return input;\n}\n\nstring serialize_stdout(const string &result) {\n return result;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n string s = deserialize_stdin();\n ...
3737
Paint House IV
paint-house-iv
<p>You are given an <strong>even</strong> integer <code>n</code> representing the number of houses arranged in a straight line, and a 2D array <code>cost</code> of size <code>n x 3</code>, where <code>cost[i][j]</code> represents the cost of painting house <code>i</code> with color <code>j + 1</code>.</p> <p>The house...
You are given an **even** integer `n` representing the number of houses arranged in a straight line, and a 2D array `cost` of size `n x 3`, where `cost[i][j]` represents the cost of painting house `i` with color `j + 1`. The houses will look **beautiful** if they satisfy the following conditions: - No **two** adjac...
Medium
[ "array", "dynamic-programming" ]
leetcode
https://leetcode.com/problems/paint-house-iv
functional
null
null
null
null
{ "c": "long long minCost(int n, int** cost, int costSize, int* costColSize) {\n \n}", "cpp": "class Solution {\npublic:\n long long minCost(int n, vector<vector<int>>& cost) {\n \n }\n};", "csharp": "public class Solution {\n public long MinCost(int n, int[][] cost) {\n \n }\n}", "...
{ "cpp": { "code": "class Solution {\npublic:\n long long minCost(int n, vector<vector<int>>& cost) {\n int m = n / 2;\n long long prev_dp[3][3];\n for (int c1 = 0; c1 < 3; ++c1)\n for (int c2 = 0; c2 < 3; ++c2)\n prev_dp[c1][c2] = LLONG_MAX;\n \n in...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random random.seed(seed) def sample_solution(n, cost): m = n // 2 dp = {} first_left, first_right = 0, n - 1 for c1 in range(3): for c2 in range(3): if c1 != c2: ...
def evaluate(expected_output: str, program_output: str) -> bool: try: # Strip whitespace and compare integer values. expected = int(expected_output.strip()) result = int(program_output.strip()) return expected == result except Exception: return False
[{"input": "2\n1,2,3\n4,5,6", "output": "6"}, {"input": "4\n3,5,7\n6,2,9\n4,8,1\n7,3,5", "output": "9"}, {"input": "6\n2,4,6\n5,3,8\n7,1,9\n4,6,2\n3,5,7\n8,2,4", "output": "18"}, {"input": "4\n10,10,10\n10,10,10\n10,10,10\n10,10,10", "output": "40"}, {"input": "8\n0,100,100\n100,0,100\n100,100,0\n0,50,100\n100,0,50\n50...
{ "cpp": "==Code Submission==\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int n;\n cin >> n;\n vector<vector<int>> cost(n, vector<int>(3));\n string dummy;\n getline(cin, dummy); // consume the rest of the line\n for (int i = 0; i < n; i++) {\n string line;\n ...
3739
Manhattan Distances of All Arrangements of Pieces
manhattan-distances-of-all-arrangements-of-pieces
<p>You are given three integers <code><font face="monospace">m</font></code>, <code><font face="monospace">n</font></code>, and <code>k</code>.</p> <p>There is a rectangular grid of size <code>m &times; n</code> containing <code>k</code> identical pieces. Return the sum of Manhattan distances between every pair of pie...
You are given three integers `m`, `n`, and `k`. There is a rectangular grid of size `m n` containing `k` identical pieces. Return the sum of Manhattan distances between every pair of pieces over all **valid arrangements** of pieces. A **valid arrangement** is a placement of all `k` pieces on the grid with **at mos...
Hard
[ "math", "combinatorics" ]
leetcode
https://leetcode.com/problems/manhattan-distances-of-all-arrangements-of-pieces
functional
null
null
null
null
{ "c": "int distanceSum(int m, int n, int k) {\n \n}", "cpp": "class Solution {\npublic:\n int distanceSum(int m, int n, int k) {\n \n }\n};", "csharp": "public class Solution {\n public int DistanceSum(int m, int n, int k) {\n \n }\n}", "dart": "class Solution {\n int distanceSum(...
{ "cpp": { "code": "class Solution {\n long long mod_pow(long long base, int exp, int mod) {\n long long result = 1;\n base %= mod;\n while (exp > 0) {\n if (exp % 2 == 1) {\n result = (result * base) % mod;\n }\n base = (base * base) % mod;\...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random random.seed(seed) MOD = 10**9 + 7 max_fact = 10**5 fact = [1] * (max_fact + 1) for i in range(1, max_fact + 1): fact[i] = fact[i - 1] * i % MOD inv_fact = [1] * (max_fact + 1) inv_fact[max_fact] =...
def evaluate(expected_output: str, program_output: str) -> bool: try: expected = int(expected_output.strip()) program = int(program_output.strip()) return expected == program except: return False
[{"input": "1,2,2", "output": "1"}, {"input": "1,4,3", "output": "20"}, {"input": "2,2,2", "output": "8"}, {"input": "2,3,6", "output": "25"}, {"input": "3,3,4", "output": "1512"}, {"input": "95895,1,27311", "output": "584005881"}, {"input": "57432,1,42454", "output": "866460139"}, {"input": "27996,3,21077", "output": ...
{ "cpp": "==Code Submission==\n\npair<int, pair<int, int>> deserialize_stdin(const string &input) {\n stringstream ss(input);\n string token;\n int m, n, k;\n getline(ss, token, ',');\n m = stoi(token);\n getline(ss, token, ',');\n n = stoi(token);\n getline(ss, token);\n k = stoi(token);\n...
3741
Reschedule Meetings for Maximum Free Time II
reschedule-meetings-for-maximum-free-time-ii
<p>You are given an integer <code>eventTime</code> denoting the duration of an event. You are also given two integer arrays <code>startTime</code> and <code>endTime</code>, each of length <code>n</code>.</p> <p>These represent the start and end times of <code>n</code> <strong>non-overlapping</strong> meetings that occ...
You are given an integer `eventTime` denoting the duration of an event. You are also given two integer arrays `startTime` and `endTime`, each of length `n`. These represent the start and end times of `n` **non\-overlapping** meetings that occur during the event between time `t = 0` and time `t = eventTime`, where the...
Medium
[ "array", "greedy", "enumeration" ]
leetcode
https://leetcode.com/problems/reschedule-meetings-for-maximum-free-time-ii
functional
null
null
null
null
{ "c": "int maxFreeTime(int eventTime, int* startTime, int startTimeSize, int* endTime, int endTimeSize) {\n \n}", "cpp": "class Solution {\npublic:\n int maxFreeTime(int eventTime, vector<int>& startTime, vector<int>& endTime) {\n \n }\n};", "csharp": "public class Solution {\n public int MaxF...
{ "cpp": { "code": "class Solution {\npublic:\n int maxFreeTime(int eventTime, vector<int>& startTime, vector<int>& endTime) {\n int n = startTime.size();\n vector<int> gaps(n + 1);\n gaps[0] = startTime[0];\n for (int i = 1; i < n; ++i) {\n gaps[i] = startTime[i] - endTi...
def generate_test_cases(num_cases: int, seed: int = 42) -> list[dict]: import random random.seed(seed) test_cases = [] def compute_max_free_time(eventTime, startTime, endTime): n = len(startTime) if n == 0: return eventTime pre_size = startTime[0] - 0 mid_si...
def evaluate(expected_output: str, program_output: str) -> bool: try: expected_val = int(expected_output.strip()) program_val = int(program_output.strip()) return expected_val == program_val except Exception: return False
[{"input": "5;1,3;2,5", "output": "2"}, {"input": "10;0,7,9;1,8,10", "output": "7"}, {"input": "10;0,3,7,9;1,4,8,10", "output": "6"}, {"input": "5;0,1,2,3,4;1,2,3,4,5", "output": "0"}, {"input": "10;0,5;5,9", "output": "1"}, {"input": "50;0,2,12,19,20,21,23,27,31,40;2,12,19,20,21,23,27,31,40,50", "output": "0"}, {"inpu...
{ "cpp": "==Code Submission==\n\nint main() {\n string input;\n getline(cin, input);\n // Expected format: eventTime;start1,start2,...;end1,end2,...\n string eventTimeStr, startStr, endStr;\n size_t pos1 = input.find(';');\n if (pos1 == string::npos) return 1;\n eventTimeStr = input.substr(0, pos...
3743
Reschedule Meetings for Maximum Free Time I
reschedule-meetings-for-maximum-free-time-i
<p>You are given an integer <code>eventTime</code> denoting the duration of an event, where the event occurs from time <code>t = 0</code> to time <code>t = eventTime</code>.</p> <p>You are also given two integer arrays <code>startTime</code> and <code>endTime</code>, each of length <code>n</code>. These represent the ...
You are given an integer `eventTime` denoting the duration of an event, where the event occurs from time `t = 0` to time `t = eventTime`. You are also given two integer arrays `startTime` and `endTime`, each of length `n`. These represent the start and end time of `n` **non\-overlapping** meetings, where the `ith` me...
Medium
[ "array", "greedy", "sliding-window" ]
leetcode
https://leetcode.com/problems/reschedule-meetings-for-maximum-free-time-i
functional
null
null
null
null
{ "c": "int maxFreeTime(int eventTime, int k, int* startTime, int startTimeSize, int* endTime, int endTimeSize) {\n \n}", "cpp": "class Solution {\npublic:\n int maxFreeTime(int eventTime, int k, vector<int>& startTime, vector<int>& endTime) {\n \n }\n};", "csharp": "public class Solution {\n p...
{ "cpp": { "code": "class Solution {\npublic:\n int maxFreeTime(int eventTime, int k, vector<int>& startTime, vector<int>& endTime) {\n vector<int> gaps;\n int n = startTime.size();\n gaps.push_back(startTime[0]);\n for (int i = 1; i < n; ++i) {\n gaps.push_back(startTime...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random random.seed(seed) test_cases = [] def sample_max_free_time(eventTime, k, startTime, endTime): n = len(startTime) gaps = [] gaps.append(startTime[0]) for i in range(n - 1): ...
def evaluate(expected_output: str, program_output: str) -> bool: try: expected_val = int(expected_output.strip()) program_val = int(program_output.strip()) return expected_val == program_val except Exception: return False
[{"input": "5 1 1,3 2,5", "output": "2"}, {"input": "10 1 0,2,9 1,4,10", "output": "6"}, {"input": "5 2 0,1,2,3,4 1,2,3,4,5", "output": "0"}, {"input": "100 2 10,30,55,70 20,40,60,80", "output": "45"}, {"input": "50 1 5,20,35 10,25,40", "output": "20"}, {"input": "100 1 1,2,3 2,3,4", "output": "96"}, {"input": "24 3 6,...
{ "cpp": "==Code Submission==\n\n \nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n \n string line;\n getline(cin, line);\n istringstream iss(line);\n \n int eventTime, k;\n string startStr, endStr;\n iss >> eventTime >> k >> startStr >> endStr;\n \n vector<int> ...
3747
Maximum Difference Between Adjacent Elements in a Circular Array
maximum-difference-between-adjacent-elements-in-a-circular-array
<p>Given a <strong>circular</strong> array <code>nums</code>, find the <b>maximum</b> absolute difference between adjacent elements.</p> <p><strong>Note</strong>: In a circular array, the first and last elements are adjacent.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-bl...
Given a **circular** array `nums`, find the **maximum** absolute difference between adjacent elements. **Note**: In a circular array, the first and last elements are adjacent. **Example 1:** **Input:** nums \= \[1,2,4] **Output:** 3 **Explanation:** Because `nums` is circular, `nums[0]` and `nums[2]` a...
Easy
[ "array" ]
leetcode
https://leetcode.com/problems/maximum-difference-between-adjacent-elements-in-a-circular-array
functional
null
null
null
null
{ "c": "int maxAdjacentDistance(int* nums, int numsSize) {\n \n}", "cpp": "class Solution {\npublic:\n int maxAdjacentDistance(vector<int>& nums) {\n \n }\n};", "csharp": "public class Solution {\n public int MaxAdjacentDistance(int[] nums) {\n \n }\n}", "dart": "class Solution {\n ...
{ "cpp": { "code": "class Solution {\npublic:\n int maxAdjacentDistance(vector<int>& nums) {\n int n = nums.size();\n int max_diff = 0;\n for (int i = 0; i < n; ++i) {\n int next = (i + 1) % n;\n int diff = abs(nums[i] - nums[next]);\n if (diff > max_diff) ...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random random.seed(seed) def sample_solution(nums): max_diff = 0 n = len(nums) for i in range(n): next_i = (i + 1) % n current_diff = abs(nums[i] - nums[next_i]) if current_di...
def evaluate(expected_output: str, program_output: str) -> bool: # Deserialization: remove surrounding whitespace # and convert the serialized output (a single integer) to int try: expected_val = int(expected_output.strip()) program_val = int(program_output.strip()) return expected_...
[{"input": "1,2,4", "output": "3"}, {"input": "-5,-10,-5", "output": "5"}, {"input": "5,5", "output": "0"}, {"input": "1,2,3,4,5", "output": "4"}, {"input": "5,4,3,2,1", "output": "4"}, {"input": "-100,0,100", "output": "200"}, {"input": "-72,-94,89,-30,-38,-43,-65,88,-74,73,89,39,-78,51,8,-92,-93,-77,-45,-41,29,54,-94...
{ "cpp": "==Code Submission==\n\n// Deserialization: parse a comma-separated list of integers.\nvector<int> deserialize_stdin(const string &input) {\n vector<int> nums;\n stringstream ss(input);\n string token;\n while(getline(ss, token, ',')) {\n if(!token.empty())\n nums.push_back(atoi...
3748
Sort Matrix by Diagonals
sort-matrix-by-diagonals
<p>You are given an <code>n x n</code> square matrix of integers <code>grid</code>. Return the matrix such that:</p> <ul> <li>The diagonals in the <strong>bottom-left triangle</strong> (including the middle diagonal) are sorted in <strong>non-increasing order</strong>.</li> <li>The diagonals in the <strong>top-right...
You are given an `n x n` square matrix of integers `grid`. Return the matrix such that: - The diagonals in the **bottom\-left triangle** (including the middle diagonal) are sorted in **non\-increasing order**. - The diagonals in the **top\-right triangle** are sorted in **non\-decreasing order**. **Example 1:**...
Medium
[ "array", "sorting", "matrix" ]
leetcode
https://leetcode.com/problems/sort-matrix-by-diagonals
functional
null
null
null
null
{ "c": "/**\n * Return an array of arrays of size *returnSize.\n * The sizes of the arrays are returned as *returnColumnSizes array.\n * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().\n */\nint** sortMatrix(int** grid, int gridSize, int* gridColSize, int* returnSize, in...
{ "cpp": { "code": "class Solution {\npublic:\n vector<vector<int>> sortMatrix(vector<vector<int>>& grid) {\n int n = grid.size();\n map<int, vector<pair<int, int>>> diagonals;\n \n for (int k = -(n-1); k <= n-1; ++k) {\n vector<pair<int, int>> cells;\n for (in...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random random.seed(seed) def serialize_grid(grid): # Serialize grid as rows separated by ";" and numbers in each row by "," return ";".join(",".join(str(x) for x in row) for row in grid) from collections im...
def evaluate(expected_output: str, program_output: str) -> bool: def deserialize(output_str): output_str = output_str.strip() if not output_str: return [] rows = output_str.split(";") grid = [] for row in rows: if row == "": grid.append...
[{"input": "1", "output": "1"}, {"input": "0,1;1,2", "output": "2,1;1,0"}, {"input": "1,7,3;9,8,2;4,5,6", "output": "8,2,3;9,6,7;4,5,1"}, {"input": "4,3,2,1;8,7,6,5;12,11,10,9;16,15,14,13", "output": "13,3,2,1;14,10,6,5;15,11,7,9;16,12,8,4"}, {"input": "-1,-2,-3,-4,-5;5,4,3,2,1;10,9,8,7,6;0,0,0,0,0;6,7,8,9,10", "output...
{ "cpp": "==Code Submission==\n \n\nvector<vector<int>> deserialize_stdin(const string &input) {\n vector<vector<int>> grid;\n size_t start = 0;\n while (start < input.size()) {\n size_t end = input.find(';', start);\n string rowStr;\n if (end == string::npos) {\n rowStr = inp...
3753
Maximum Difference Between Even and Odd Frequency I
maximum-difference-between-even-and-odd-frequency-i
<p>You are given a string <code>s</code> consisting of lowercase English letters. Your task is to find the <strong>maximum</strong> difference between the frequency of <strong>two</strong> characters in the string such that:</p> <ul> <li>One of the characters has an <strong>even frequency</strong> in the string.</li>...
You are given a string `s` consisting of lowercase English letters. Your task is to find the **maximum** difference between the frequency of **two** characters in the string such that: - One of the characters has an **even frequency** in the string. - The other character has an **odd frequency** in the string. Retu...
Easy
[ "hash-table", "string", "counting" ]
leetcode
https://leetcode.com/problems/maximum-difference-between-even-and-odd-frequency-i
functional
null
null
null
null
{ "c": "int maxDifference(char* s) {\n \n}", "cpp": "class Solution {\npublic:\n int maxDifference(string s) {\n \n }\n};", "csharp": "public class Solution {\n public int MaxDifference(string s) {\n \n }\n}", "dart": "class Solution {\n int maxDifference(String s) {\n \n }\n}"...
{ "cpp": { "code": "class Solution {\npublic:\n int maxDifference(string s) {\n vector<int> freq(26, 0);\n for (char c : s) {\n freq[c - 'a']++;\n }\n \n vector<int> evens, odds;\n for (int i = 0; i < 26; ++i) {\n if (freq[i] == 0) continue;\n ...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random from collections import Counter random.seed(seed) def sample_solution(s: str) -> int: freq = Counter(s) even = [] odd = [] for v in freq.values(): if v % 2 == 0: ...
def evaluate(expected_output: str, program_output: str) -> bool: try: exp_val = int(expected_output.strip()) prog_val = int(program_output.strip()) return exp_val == prog_val except Exception: return False
[{"input": "aab", "output": "-1"}, {"input": "aaaaabbc", "output": "3"}, {"input": "abcabcab", "output": "1"}, {"input": "aabbc", "output": "-1"}, {"input": "abbccc", "output": "1"}, {"input": "zzzzzyy", "output": "3"}, {"input": "aabbccd", "output": "-1"}, {"input": "ctgdctopacgpossklhwtekhftcjjigbldxchqxjebruzwwjlvej...
{ "cpp": "==Code Submission==\nstring deserialize_stdin() {\n string s;\n getline(cin, s);\n return s;\n}\n\nstring serialize_stdout(int result) {\n return to_string(result);\n}\n\nint main() {\n string s = deserialize_stdin();\n Solution sol;\n int ans = sol.maxDifference(s);\n cout << serial...
3760
Assign Elements to Groups with Constraints
assign-elements-to-groups-with-constraints
<p>You are given an integer array <code>groups</code>, where <code>groups[i]</code> represents the size of the <code>i<sup>th</sup></code> group. You are also given an integer array <code>elements</code>.</p> <p>Your task is to assign <strong>one</strong> element to each group based on the following rules:</p> <ul> ...
You are given an integer array `groups`, where `groups[i]` represents the size of the `ith` group. You are also given an integer array `elements`. Your task is to assign **one** element to each group based on the following rules: - An element at index `j` can be assigned to a group `i` if `groups[i]` is **divisible...
Medium
[ "array", "hash-table" ]
leetcode
https://leetcode.com/problems/assign-elements-to-groups-with-constraints
functional
null
null
null
null
{ "c": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* assignElements(int* groups, int groupsSize, int* elements, int elementsSize, int* returnSize) {\n \n}", "cpp": "class Solution {\npublic:\n vector<int> assignElements(vector<int>& groups, vector<int>& elements) {...
{ "cpp": { "code": "class Solution {\npublic:\n vector<int> assignElements(vector<int>& groups, vector<int>& elements) {\n const int MAX_ELEMENT = 100000;\n vector<int> element_indices(MAX_ELEMENT + 1, -1);\n for (int i = 0; i < elements.size(); ++i) {\n int e = elements[i];\n ...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random, math random.seed(seed) def serialize_list(lst): return ",".join(map(str, lst)) # This function implements the provided sample solution. def sample_solution(groups, elements): element_map = {} fo...
def evaluate(expected_output: str, program_output: str) -> bool: # Deserializes a comma-separated list of integers. def deserialize_output(s): s = s.strip() return [] if s == "" else list(map(int, s.split(","))) expected = deserialize_output(expected_output) program = deserialize_output(...
[{"input": "1 1", "output": "0"}, {"input": "8,4,3,2,4 4,2", "output": "0,0,-1,1,0"}, {"input": "2,3,5,7 5,3,3", "output": "-1,1,0,-1"}, {"input": "10,21,30,41 2,1", "output": "0,1,0,1"}, {"input": "12,15,18,20 2,3,5", "output": "0,1,0,0"}, {"input": "97197,36049 32099", "output": "-1,-1"}, {"input": "96531,13435,88697...
{ "cpp": "==Code Submission==\n\npair<vector<int>, vector<int>> deserialize_stdin(const string &input) {\n size_t pos = input.find(' ');\n string groups_str = (pos == string::npos) ? input : input.substr(0, pos);\n string elements_str = (pos == string::npos) ? \"\" : input.substr(pos + 1);\n vector<int> g...
3763
Separate Squares I
separate-squares-i
<p>You are given a 2D integer array <code>squares</code>. Each <code>squares[i] = [x<sub>i</sub>, y<sub>i</sub>, l<sub>i</sub>]</code> represents the coordinates of the bottom-left point and the side length of a square parallel to the x-axis.</p> <p>Find the <strong>minimum</strong> y-coordinate value of a horizontal ...
You are given a 2D integer array `squares`. Each `squares[i] = [xi, yi, li]` represents the coordinates of the bottom\-left point and the side length of a square parallel to the x\-axis. Find the **minimum** y\-coordinate value of a horizontal line such that the total area of the squares above the line *equals* the t...
Medium
[ "array", "binary-search" ]
leetcode
https://leetcode.com/problems/separate-squares-i
functional
null
null
null
null
{ "c": "double separateSquares(int** squares, int squaresSize, int* squaresColSize) {\n \n}", "cpp": "class Solution {\npublic:\n double separateSquares(vector<vector<int>>& squares) {\n \n }\n};", "csharp": "public class Solution {\n public double SeparateSquares(int[][] squares) {\n \n...
{ "cpp": { "code": "class Solution {\npublic:\n double separateSquares(vector<vector<int>>& squares) {\n long long total_area = 0;\n double max_y_high = 0.0;\n for (auto& sq : squares) {\n int yi = sq[1];\n int li = sq[2];\n total_area += static_cast<long l...
def generate_test_cases(num_cases: int, seed: int = 42) -> list[dict]: import random random.seed(seed) # This helper function is an adaptation of the provided sample solution. def sample_solution(squares): total_area = 0.0 max_y = 0 for x, y, l in squares: total_area...
def evaluate(expected_output: str, program_output: str) -> bool: try: # Deserialization: strip whitespace and convert to float. expected_val = float(expected_output.strip()) program_val = float(program_output.strip()) # Check if the outputs match within an absolute tolerance. ...
[{"input": "1\n0 0 1", "output": "0.50000"}, {"input": "2\n0 0 1\n2 2 1", "output": "1.00000"}, {"input": "2\n0 0 2\n1 1 1", "output": "1.16667"}, {"input": "2\n0 0 1\n2 0 3", "output": "1.33333"}, {"input": "1\n1000000000 1000000000 1", "output": "1000000000.50000"}, {"input": "3\n0 0 3\n1 2 2\n2 1 1", "output": "2.00...
{ "cpp": "==Code Submission==\n\nvector<vector<int>> deserialize_stdin(const string &input) {\n vector<vector<int>> squares;\n istringstream iss(input);\n int n;\n iss >> n;\n for (int i = 0; i < n; i++) {\n int x, y, l;\n iss >> x >> y >> l;\n squares.push_back({x, y, l});\n }\...
3768
Check If Digits Are Equal in String After Operations I
check-if-digits-are-equal-in-string-after-operations-i
<p>You are given a string <code>s</code> consisting of digits. Perform the following operation repeatedly until the string has <strong>exactly</strong> two digits:</p> <ul> <li>For each pair of consecutive digits in <code>s</code>, starting from the first digit, calculate a new digit as the sum of the two digits <str...
You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits: - For each pair of consecutive digits in `s`, starting from the first digit, calculate a new digit as the sum of the two digits **modulo** 10\. - Replace `s` with the sequence of ne...
Easy
[ "math", "string", "simulation", "combinatorics", "number-theory" ]
leetcode
https://leetcode.com/problems/check-if-digits-are-equal-in-string-after-operations-i
functional
null
null
null
null
{ "c": "bool hasSameDigits(char* s) {\n \n}", "cpp": "class Solution {\npublic:\n bool hasSameDigits(string s) {\n \n }\n};", "csharp": "public class Solution {\n public bool HasSameDigits(string s) {\n \n }\n}", "dart": "class Solution {\n bool hasSameDigits(String s) {\n \n }...
{ "cpp": { "code": "class Solution {\npublic:\n bool hasSameDigits(string s) {\n while (s.size() > 2) {\n string next;\n for (int i = 0; i < s.size() - 1; ++i) {\n int sum = (s[i] - '0') + (s[i+1] - '0');\n next.push_back((sum % 10) + '0');\n ...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random random.seed(seed) # Helper: sample solution based on the provided sample solution logic. def sample_solution(s: str) -> bool: current = [int(ch) for ch in s] while len(current) > 2: new_curren...
def evaluate(expected_output: str, program_output: str) -> bool: # Deserialize by stripping extra whitespace and converting to lower-case. expected = expected_output.strip().lower() actual = program_output.strip().lower() return expected == actual
[{"input": "3902", "output": "true"}, {"input": "34789", "output": "false"}, {"input": "123", "output": "false"}, {"input": "111", "output": "true"}, {"input": "909", "output": "true"}, {"input": "000", "output": "true"}, {"input": "13579", "output": "false"}, {"input": "24680", "output": "false"}, {"input": "987654321...
{ "cpp": "==Code Submission==\n\nstring deserialize_stdin() {\n string s;\n getline(cin, s);\n return s;\n}\n\nstring serialize_stdout(bool result) {\n return result ? \"true\" : \"false\";\n}\n\nint main() {\n string s = deserialize_stdin();\n Solution sol; // The submitted solution will be injecte...
3774
Check If Digits Are Equal in String After Operations II
check-if-digits-are-equal-in-string-after-operations-ii
<p>You are given a string <code>s</code> consisting of digits. Perform the following operation repeatedly until the string has <strong>exactly</strong> two digits:</p> <ul> <li>For each pair of consecutive digits in <code>s</code>, starting from the first digit, calculate a new digit as the sum of the two digits <str...
You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits: - For each pair of consecutive digits in `s`, starting from the first digit, calculate a new digit as the sum of the two digits **modulo** 10\. - Replace `s` with the sequence of ne...
Hard
[ "math", "string", "combinatorics", "number-theory" ]
leetcode
https://leetcode.com/problems/check-if-digits-are-equal-in-string-after-operations-ii
functional
null
null
null
null
{ "c": "bool hasSameDigits(char* s) {\n \n}", "cpp": "class Solution {\npublic:\n bool hasSameDigits(string s) {\n \n }\n};", "csharp": "public class Solution {\n public bool HasSameDigits(string s) {\n \n }\n}", "dart": "class Solution {\n bool hasSameDigits(String s) {\n \n }...
{ "cpp": { "code": "class Solution {\npublic:\n bool hasSameDigits(string s) {\n vector<int> digits;\n for (char c : s) {\n digits.push_back(c - '0');\n }\n int m = digits.size() - 2;\n vector<int> coeff(m + 1);\n for (int k = 0; k <= m; ++k) {\n ...
def generate_test_cases(num_cases: int, seed: int = 42) -> list: import random random.seed(seed) # Sample solution adapted from the provided sample solution. def sample_solution(s: str) -> bool: n = len(s) if n == 2: return s[0] == s[1] k = n - 2 mod5_table ...
def evaluate(expected_output: str, program_output: str) -> bool: # Use the same simple deserialization: strip whitespace and lower-case. expected = expected_output.strip().lower() program = program_output.strip().lower() return expected == program
[{"input": "3902", "output": "true"}, {"input": "34789", "output": "false"}, {"input": "123", "output": "false"}, {"input": "111", "output": "true"}, {"input": "909", "output": "true"}, {"input": "55555", "output": "true"}, {"input": "123456", "output": "false"}, {"input": "987654321", "output": "false"}, {"input": "31...
{ "cpp": "==Code Submission==\n\nstring deserialize_stdin(const string &input) {\n // For our problem, the entire input is the string itself.\n return input;\n}\n\nstring serialize_stdout(bool result) {\n return result ? \"true\" : \"false\";\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(n...