First 10 rows of cpp
| uuidstring | contentstring | content_formatstring | raw_contentstring | taskstring | analysisstring | solutionstring | teststring | full_contentstring | full_content_formatstring |
|---|---|---|---|---|---|---|---|---|---|
| 140ebc29-af24-4f87-97ac-5e3be649c997 | /* Write a C++ function that accepts a square matrix represented as a vector of vectors of integers and returns a vector of integers containing the matrix elements in a zigzag diagonal order, starting from the top-left element and proceeding along anti-diagonals, alternating dire… | code comment | //{ Driver Code Starts #include<bits/stdc++.h> using namespace std; // } Driver Code Ends /*You are required to complete this method */ class Solution{ public: vector<int> matrixDiagonally(vector<vector<int>>&mat) { //Your code here /* … |
| Write a C++ function that accepts a square matrix represented as a vector of vectors of integers and returns a vector of integers containing the matrix elements in a zigzag diagonal order, starting from the top-left element and proceeding along anti-diagonals, alternating directi… |
| The algorithm simulates the zigzag traversal by tracking a current position (row, column) and moving diagonally in alternating directions. Starting at (0,0), the movement pattern is: move one step right (or down if at the right edge) then traverse the descending anti-diagonal (mo… |
| #include <vector> // Returns the elements of a square matrix in zigzag diagonal order. std::vector<int> matrixDiagonally(const std::vector<std::vector<int>>& mat) { int n = static_cast<int>(mat.size()); std::vector<int> result; if (n == 0) return result; int… |
| #include <cassert> #include <vector> // Declaration of the function being tested (normally in a header) std::vector<int> matrixDiagonally(const std::vector<std::vector<int>>& mat); int main() { // Test 1: 1x1 matrix std::vector<std::vector<int>> m1 = {{7}}; assert(m… |
| /* Write a C++ function that accepts a square matrix represented as a vector of vectors of integers and returns a vector of integers containing the matrix elements in a zigzag diagonal order, starting from the top-left element and proceeding along anti-diagonals, alternating dire… |
| solution-first order |
| 1784b2be-72a8-488e-9860-59afe3c609fa | /* You are given an `N x N` grid (1 ≤ N ≤ 16) where each cell is either empty (0) or blocked by a wall (1). A pipe must be moved from the top-left corner (cell (0,0)) to the bottom-right corner (cell (N-1,N-1)), starting with a horizontal segment occupying cells (0,0) and (0,1). … | code comment | #include <iostream> #include <queue> using namespace std; int N; int field[16][16]; int dp[16][16][3]; int getValue(int r, int c, int dir) { /* already visited */ // cout << r << ':' << c << ':' << dir << '\n'; if (dp[r][c][dir] != -1) { return dp[r][c][di… | You are given an `N x N` grid (1 ≤ N ≤ 16) where each cell is either empty (0) or blocked by a wall (1). A pipe must be moved from the top-left corner (cell (0,0)) to the bottom-right corner (cell (N-1,N-1)), starting with a horizontal segment occupying cells (0,0) and (0,1). The… | The problem reduces to counting paths in a state space where each state is defined by the head position (r,c) and orientation `dir` (0 = horizontal, 1 = diagonal, 2 = vertical). The pipe occupies two cells for horizontal/vertical and four cells for diagonal. We use memoization (d… | #include <vector> #include <cstring> // Count the number of ways to move a pipe from start to bottom-right corner. // field[r][c] == 1 means wall (blocked). N is grid size (1..16). // Orientation: 0 = horizontal, 1 = diagonal, 2 = vertical. int countPaths(int N, const std::vecto… | #include <cassert> #include <vector> // (solution function above) int main() { // Test 1: 3x3 empty grid, should have 1 path (horizontal, then diagonal, then vertical) std::vector<std::vector<int>> field1 = { {0,0,0}, {0,0,0}, {0,0,0} }; … | // You are given an `N x N` grid (1 ≤ N ≤ 16) where each cell is either empty (0) or blocked by a wall (1). A pipe must be moved from the top-left corner (cell (0,0)) to the bottom-right corner (cell (N-1,N-1)), starting with a horizontal segment occupying cells (0,0) and (0,1). … | standard order |
| 73d62f83-e8d8-497e-9825-d8ef2cca8df0 | /* Given two integers `T` (number of true statements) and `F` (number of false statements), write a C++ function `maxStatements` that returns the maximum number of statements that can appear in a valid sequence, or -1 if no valid sequence exists. A sequence is valid if it consist… | code comment | #include <bits/stdc++.h> using namespace std; #define int long long #define nL "\n" #define pb push_back #define mk make_pair #define F first #define S second #define all(x) (x).begin(), (x).end() #define forEqual(i, a, b) for (int i = a; i <= b; i++) #define loop(i, a, b) for (i… | Given two integers `T` (number of true statements) and `F` (number of false statements), write a C++ function `maxStatements` that returns the maximum number of statements that can appear in a valid sequence, or -1 if no valid sequence exists. A sequence is valid if it consists o… | The core observation is that true statements cannot be adjacent unless the second true is the very last element of the sequence. Since each true (except possibly the last) needs a false immediately after it, each false can separate at most two true statements (one before and one … | #include <algorithm> // Return the maximum number of statements in a valid sequence, or -1 if none. long long maxStatements(long long T, long long F) { if (F >= T) { return -1; } return std::min(2LL * F + 1LL, T + F); } | #include <cassert> // Solution function declared here (for completeness, not needed in test) long long maxStatements(long long, long long); int main() { // Basic cases assert(maxStatements(1, 0) == 1); assert(maxStatements(2, 1) == 3); // 2*1+1=3, T+F=3 -> 3 ass… | // Given two integers `T` (number of true statements) and `F` (number of false statements), write a C++ function `maxStatements` that returns the maximum number of statements that can appear in a valid sequence, or -1 if no valid sequence exists. A sequence is valid if it consist… | standard order |
| 8d341568-4c54-47f0-997c-175d9968a872 | /* Write a C++ function `int maxMatchingApartments(int n, int m, int k, const std::vector<int>& applicants, const std::vector<int>& apartmentSizes)` that determines the maximum number of applicants who can each be assigned an apartment, given that the desired apartment size of an… | code comment | #include <iostream> #include <bits/stdc++.h> using namespace std; int main(){ int n, m, k, num; vector<int> applicants; vector<int> sizes; cin >> n >> m >> k; for(int i = 0; i < n; i++){ cin >> num; applicants.push_back(num); } for(in… | Write a C++ function `int maxMatchingApartments(int n, int m, int k, const std::vector<int>& applicants, const std::vector<int>& apartmentSizes)` that determines the maximum number of applicants who can each be assigned an apartment, given that the desired apartment size of an ap… | The problem is a classic bipartite matching problem where you want to maximize the number of pairs such that the difference between an applicant's desired size and an apartment's actual size is within `k`. The greedy approach works because both lists are sorted: iterate with two … | #include <vector> #include <algorithm> #include <cstdlib> // for std::abs // Returns the maximum number of applicants that can be assigned an apartment // where the absolute difference between desired size and apartment size <= k. // The input vectors are not modified. int maxMa… | #include <cassert> #include <vector> int main() { // Test case 1: Simple match with k=5. std::vector<int> app1 = {60, 45, 80}; std::vector<int> ap1 = {30, 60, 75}; assert(maxMatchingApartments(3, 3, 5, app1, ap1) == 1); // only 60-60 // Test case 2: Larger k… | // Write a C++ function `int maxMatchingApartments(int n, int m, int k, const std::vector<int>& applicants, const std::vector<int>& apartmentSizes)` that determines the maximum number of applicants who can each be assigned an apartment, given that the desired apartment size of an… | standard order |
| 359fce7d-0449-4eb1-a7f2-4f513d073043 | /* Write a C++ function named `netSalary` that accepts a gross annual salary as a non-negative integer and returns the net (after-tax) salary as a `uint32_t`, using a progressive tax system with the exact same brackets and rates as shown in the snippet: the first 13,000 of income… | code comment | #include <iostream> #include <stdlib.h> #include <time.h> #include <vector> #include <typeinfo> #include <stdexcept> uint32_t SalaryAfterTax(int BaseSalary) { // Brackets and variables, can be tweaked. uint32_t TopBracket{100000}, MiddleBracket{50000}, LowerBracket{13… | Write a C++ function named `netSalary` that accepts a gross annual salary as a non-negative integer and returns the net (after-tax) salary as a `uint32_t`, using a progressive tax system with the exact same brackets and rates as shown in the snippet: the first 13,000 of income is… | The solution uses a recursive approach that mirrors the original snippet. The algorithm checks the input salary against four increasing thresholds (13,000, 50,000, 100,000). For each bracket above the base, it computes the tax‑free portion of that bracket’s income (the difference… | #include <cstdint> // Computes net salary after progressive tax based on salary brackets. // Brackets: 0-13,000 tax-free; 13,001-50,000 at 30%; 50,001-100,000 at 45%; above 100,000 at 55%. // Recursive implementation consistent with the original snippet. uint32_t netSalary(int b… | #include <cassert> #include <cstdint> // Declaration of the function under test (already defined above in solution). uint32_t netSalary(int baseSalary); int main() { // Basic cases: no tax for 13,000 or less. assert(netSalary(0) == 0); assert(netSalary(13000) == 130… | // Write a C++ function named `netSalary` that accepts a gross annual salary as a non-negative integer and returns the net (after-tax) salary as a `uint32_t`, using a progressive tax system with the exact same brackets and rates as shown in the snippet: the first 13,000 of income… | standard order |
| e5e33d1a-b343-447a-8e51-26260e87e8db | Write a C++ function `vector<vector<int>> permute(const vector<int>& nums)` that takes a vector of distinct integers (no duplicates) and returns a vector containing all possible permutations of those integers. The order of permutations in the result does not matter. The function … | no fence | //46.全排列 #if 0 /* 给定一个没有重复数字的序列,返回其所有可能的全排列。 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] class Solution { public: vector<vector<int>> permute(vector<int>& nums) { } }; */ class Solution { public: vector<vector<int>> permute(vec… | Write a C++ function `vector<vector<int>> permute(const vector<int>& nums)` that takes a vector of distinct integers (no duplicates) and returns a vector containing all possible permutations of those integers. The order of permutations in the result does not matter. The function … | The core algorithm is a backtracking approach that generates permutations by recursively fixing elements at each position. We create a **copy** of the input vector internally (since the parameter is `const`), then use a helper recursive function that takes the working copy, the r… | #include <vector> #include <algorithm> // Generate all permutations of a vector of distinct integers. // Returns a vector of vectors, each inner vector being one permutation. std::vector<std::vector<int>> permute(const std::vector<int>& nums) { std::vector<std::vector<int>> … | #include <cassert> #include <vector> #include <algorithm> // The solution function is declared above (permute) — assumed available. int main() { // Test 1: Basic case with 3 elements std::vector<int> nums1 = {1, 2, 3}; auto result1 = permute(nums1); assert(resul… | Write a C++ function `vector<vector<int>> permute(const vector<int>& nums)` that takes a vector of distinct integers (no duplicates) and returns a vector containing all possible permutations of those integers. The order of permutations in the result does not matter. The function … | standard order |
| 29470459-e816-476d-8724-f9ab84314e0e | /* You are given four integers: `a` (the time the train departs from station A), `b` (the time the train arrives at station B), `c` (the delay in minutes if the train is late, applying only when it arrives after `a` but not later than `b`), and `x` (the actual arrival time at sta… | code comment | #include<bits/stdc++.h> using namespace std; int main() { int a, b, c, x; cin >> a >> b >> c >> x; if (x <= a) {printf("%.12lf\n", 1.0);} else if (x <= b) { double res = c; res /= (b - a); printf("%.12lf\n", res); } else { … | You are given four integers: `a` (the time the train departs from station A), `b` (the time the train arrives at station B), `c` (the delay in minutes if the train is late, applying only when it arrives after `a` but not later than `b`), and `x` (the actual arrival time at statio… | The problem is a straightforward piecewise linear mapping from input `x` to a probability value. The main algorithm is a simple conditional check: first, if `x` is less than or equal to `a`, return 1.0 (since the train is definitely on time when arrival is at or before the origin… | #include <algorithm> // for std::min, std::max (though not needed, kept for completeness) // Compute the probability that the train is on time given arrival time x. // Preconditions: 0 <= a < b, 0 <= c <= b - a, and x is non-negative. double probabilityOnTime(int a, int b, int … | #include <cassert> #include <cmath> int main() { // Basic case: a=1, b=5, c=2, x=3 (between a and b) -> 2/(5-1)=0.5 assert(std::abs(probabilityOnTime(1, 5, 2, 3) - 0.5) < 1e-12); // x exactly at a -> 1.0 assert(probabilityOnTime(1, 5, 2, 1) == 1.0); // x less… | You are given four integers: `a` (the time the train departs from station A), `b` (the time the train arrives at station B), `c` (the delay in minutes if the train is late, applying only when it arrives after `a` but not later than `b`), and `x` (the actual arrival time at statio… | commented standard order |
| a6de7cbe-9f22-43a4-848f-be8fb53226d6 | /* Write a C++ function `computeModelEvidence` that takes as input a prior count `alpha0`, a prior mean `m0`, a prior scale `kappa0`, a prior degrees of freedom `v0`, a prior precision matrix `M0` (symmetric positive definite), and a data matrix `X` of size `n x d`, where `n` is … | code comment | /* * @file mixGaussEvidence.cpp * @Description Variational lower bound of the model evidence( log of marginal likelihood) * This function implements the method in the book Patterns and Recognition with Machine Learning. It is equivalent * to mixGaussVb function. * @Eqns #10.… | Write a C++ function `computeModelEvidence` that takes as input a prior count `alpha0`, a prior mean `m0`, a prior scale `kappa0`, a prior degrees of freedom `v0`, a prior precision matrix `M0` (symmetric positive definite), and a data matrix `X` of size `n x d`, where `n` is the… | The solution implements the variational Bayes algorithm for a Gaussian mixture model. The main steps are: 1. **Initialization**: Set responsibilities `r` uniformly (`n x k`). Initialize hyperparameters: `alpha = alpha0 * ones(k)`, `beta = kappa0 * ones(k)`, `m = repmat(m0, k, 1)`… | #include <Eigen/Dense> #include <Eigen/Cholesky> #include <cmath> #include <stdexcept> // Helper: digamma function (approximation using recursion and series) static double digamma(double x) { double result = 0.0; while (x < 7.0) { result -= 1.0 / x; x += … | #include <cassert> #include <cmath> #include <iostream> #include <Eigen/Dense> // Include the solution function (assume it is in the same file or included) int main() { // Test 1: Simple 2D data with 20 points (n > d) Eigen::MatrixXd X(20, 2); X << 0.1, 0.2, … | /* Write a C++ function `computeModelEvidence` that takes as input a prior count `alpha0`, a prior mean `m0`, a prior scale `kappa0`, a prior degrees of freedom `v0`, a prior precision matrix `M0` (symmetric positive definite), and a data matrix `X` of size `n x d`, where `n` is … | solution-first order |
| 38dace56-03a7-4f35-8af1-83b5fc887797 | Given a non-empty vector of unique integers and a separate vector of integer "keys" (some of which may not be present in the data vector), write a C++ function that returns a new vector containing the input data sorted in ascending order by the values of the keys, but with a twis… | markdown | // // Created by ziqi on 2024/8/5. // #include <unistd.h> #include "common/config.h" #include "executor_sort.h" static long long sort_result_fresh_id_ = 0; #define SORT_FILE_PATH(obj_name) FILE_NAME(TMP_DIR, obj_name, TMP_SUFFIX) namespace wsdb { SortExecutor::SortExecutor(Abst… | Given a non-empty vector of unique integers and a separate vector of integer "keys" (some of which may not be present in the data vector), write a C++ function that returns a new vector containing the input data sorted in ascending order by the values of the keys, but with a twis… | The core challenge is to sort elements that have a valid key (present in the key set) by their key values, while moving elements without a valid key to the end, preserving their original relative order. We first build a `std::unordered_set<int>` (or `std::set`) from the keys vect… | #include <vector> #include <unordered_set> #include <algorithm> #include <utility> // Sort data by keys: elements whose value is present in the keys set are sorted // in ascending order by that key value (stable), and all other elements are // appended at the end in their origin… | #include <cassert> #include <vector> #include <iostream> // Include the solution function here (or include a header) – for brevity, place it above. int main() { // Basic case with both existing and missing keys. std::vector<int> data = {3, 1, 4, 2}; std::vector<int>… | // Given a non-empty vector of unique integers and a separate vector of integer "keys" (some of which may not be present in the data vector), write a C++ function that returns a new vector containing the input data sorted in ascending order by the values of the keys, but with a t… | test-first order |
| 79896723-1155-4e42-9927-6921a8dee7c7 | "/*\nYou are given a rectangular grid of size \\( n \\times m \\) where each cell is either a wall ( | code comment | "#include <iostream>\n#include <string>\n#include <queue>\nusing namespace std;\nint n,m,p;\nconst i | "You are given a rectangular grid of size \\( n \\times m \\) where each cell is either a wall (`#`) | "The given snippet uses a multi‑source BFS per player, but expands exactly `speed` steps per turn, | "#include <vector>\n#include <string>\n#include <queue>\n#include <utility>\n\n// Expands all player | "#include <cassert>\n#include <vector>\n#include <string>\n\n// The solution function declaration (a | "// You are given a rectangular grid of size \\( n \\times m \\) where each cell is either a wall (` | standard order |