judge4c/prisma/seed-cn.ts

1106 lines
32 KiB
TypeScript
Raw Normal View History

2025-04-15 16:22:15 +00:00
import { PrismaClient, Prisma, EditorLanguage, LanguageServerProtocol } from "@/generated/client";
const prisma = new PrismaClient();
const editorLanguageConfigData: Prisma.EditorLanguageConfigCreateInput[] = [
{
language: EditorLanguage.c,
label: "C",
fileName: "main",
fileExtension: ".c",
languageServerConfig: {
create: {
protocol: LanguageServerProtocol.ws,
hostname: "localhost",
port: 4594,
path: "/clangd",
},
},
dockerConfig: {
create: {
image: "gcc",
tag: "latest",
workingDir: "/src",
compileOutputLimit: 1 * 1024 * 1024,
runOutputLimit: 1 * 1024 * 1024,
},
},
},
{
language: EditorLanguage.cpp,
label: "C++",
fileName: "main",
fileExtension: ".cpp",
languageServerConfig: {
create: {
protocol: LanguageServerProtocol.ws,
hostname: "localhost",
port: 4595,
path: "/clangd",
},
},
dockerConfig: {
create: {
image: "gcc",
tag: "latest",
workingDir: "/src",
compileOutputLimit: 1 * 1024 * 1024,
runOutputLimit: 1 * 1024 * 1024,
},
},
},
];
const userData: Prisma.UserCreateInput[] = [
{
name: "cfngc4594",
email: "cfngc4594@gmail.com",
password: "$2b$10$edWXpq2TOiiGQkPOXWKGlO4EKnp2YyV7OoS2qqk/W0E6GyiVQIC66",
role: "ADMIN",
problems: {
create: [
{
displayId: 1000,
title: "两数之和",
description: `#### 1. 两数之和
---
\`nums\` 和一个整数目标值 \`target\`,请你在该数组中找出 **和为目标值** _\`target\`_  的那  **两个**  整数,并返回它们的数组下标。
使
** 1**
\`\`\`
nums = [2,7,11,15], target = 9
[0,1]
nums[0] + nums[1] == 9 [0, 1]
\`\`\`
** 2**
\`\`\`
nums = [3,2,4], target = 6
[1,2]
\`\`\`
** 3**
\`\`\`
nums = [3,3], target = 6
[0,1]
\`\`\`
****
* \`2 <= nums.length <= 10^4\`
* \`-10^9 <= nums[i] <= 10^9\`
* \`-10^9 <= target <= 10^9\`
* ****
**** \`O(n^2)\` 的算法吗?
---
\`\`\`C++
\`\`\``,
solution: `<VideoEmbed platform="youtube" id="tSI98g3PDyE" />
##
###
x target - x
使 target - x x x 使 x target - x
###
\`\`\`c showLineNumbers
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
struct hashTable {
int key;
int value;
UT_hash_handle hh;
} *hashTable = NULL, *item, *tmpItem;
for (int i = 0; i < numsSize; i++) {
HASH_FIND_INT(hashTable, &nums[i], item);
if (item) {
int* result = malloc(sizeof(int) * 2);
result[0] = item->value;
result[1] = i;
*returnSize = 2;
HASH_ITER(hh, hashTable, item, tmpItem) {
HASH_DEL(hashTable, item);
free(item);
}
return result;
}
item = malloc(sizeof(struct hashTable));
item->key = target - nums[i];
item->value = i;
HASH_ADD_INT(hashTable, key, item);
}
HASH_ITER(hh, hashTable, item, tmpItem) {
HASH_DEL(hashTable, item);
free(item);
}
*returnSize = 0;
// If no valid pair is found, return an empty array
return malloc(sizeof(int) * 0);
}
\`\`\`
###
- **:** $O(n^2)$.
$n$
- **:** $O(1)$.
input 使
---
##
###
target - x
使 target - x $O(N)$ $O(1)$
x target - x x x
###
\`\`\`c showLineNumbers
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
struct hashTable {
int key;
int value;
UT_hash_handle hh;
} *hashTable = NULL, *item, *tmpItem;
for (int i = 0; i < numsSize; i++) {
HASH_FIND_INT(hashTable, &nums[i], item);
if (item) {
int* result = malloc(sizeof(int) * 2);
result[0] = item->value;
result[1] = i;
*returnSize = 2;
HASH_ITER(hh, hashTable, item, tmpItem) {
HASH_DEL(hashTable, item);
free(item);
}
return result;
}
item = malloc(sizeof(struct hashTable));
item->key = target - nums[i];
item->value = i;
HASH_ADD_INT(hashTable, key, item);
}
HASH_ITER(hh, hashTable, item, tmpItem) {
HASH_DEL(hashTable, item);
free(item);
}
*returnSize = 0;
// If no valid pair is found, return an empty array
return malloc(sizeof(int) * 0);
}
\`\`\`
###
- **:** $O(n)$.
$N$ x $O(1)$ target - x
- **:** $O(n)$.
$N$
`,
difficulty: "EASY",
published: true,
templates: {
create: [
{
language: "c",
template: `
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int *parseIntArray(char *line, int *len) {
line[strcspn(line, "\\n")] = 0;
char *p = line;
while (*p && (*p == '[' || *p == ' ' || *p == ']'))
p++;
int capacity = 10;
int *arr = malloc(capacity * sizeof(int));
*len = 0;
char *token = strtok(p, ",");
while (token) {
if (*len >= capacity) {
capacity *= 2;
arr = realloc(arr, capacity * sizeof(int));
}
arr[(*len)++] = atoi(token);
token = strtok(NULL, ",");
}
return arr;
}
char *formatOutput(int *res, int resLen) {
if (resLen == 0)
return "[]";
char *buf = malloc(resLen * 12 + 3);
char *p = buf;
*p++ = '[';
for (int i = 0; i < resLen; i++) {
p += sprintf(p, "%d", res[i]);
if (i != resLen - 1)
*p++ = ',';
}
*p++ = ']';
*p = 0;
return buf;
}
int *twoSum(int *nums, int numsSize, int target, int *returnSize);
int main() {
char line[1024];
while (fgets(line, sizeof(line), stdin)) {
int numsSize;
int *nums = parseIntArray(line, &numsSize);
if (!fgets(line, sizeof(line), stdin))
break;
int target = atoi(line);
int returnSize;
int *res = twoSum(nums, numsSize, target, &returnSize);
char *output = formatOutput(res, returnSize);
printf("%s\\n", output);
free(nums);
if (returnSize > 0)
free(res);
if (returnSize > 0)
free(output);
}
return 0;
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
}`,
},
{
language: "cpp",
template: `
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <unordered_map>
using namespace std;
// 解析输入字符串为整数数组
vector<int> parseIntArray(string line) {
vector<int> result;
line.erase(remove(line.begin(), line.end(), '['), line.end());
line.erase(remove(line.begin(), line.end(), ']'), line.end());
stringstream ss(line);
string token;
while (getline(ss, token, ',')) {
if (!token.empty()) {
result.push_back(stoi(token));
}
}
return result;
}
// 格式化输出结果为字符串
string formatOutput(const vector<int>& res) {
if (res.empty()) return "[]";
stringstream ss;
ss << "[";
for (size_t i = 0; i < res.size(); ++i) {
ss << res[i];
if (i != res.size() - 1)
ss << ",";
}
ss << "]";
return ss.str();
}
// Solution 类声明
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target);
};
int main() {
string line;
while (getline(cin, line)) {
vector<int> nums = parseIntArray(line);
if (!getline(cin, line)) break;
int target = stoi(line);
Solution sol;
vector<int> res = sol.twoSum(nums, target);
cout << formatOutput(res) << endl;
}
return 0;
}
vector<int> Solution::twoSum(vector<int>& nums, int target) {
return {};
}
`,
},
],
},
testcases: {
create: [
{
data: {
create: [
{ label: "nums", value: "[2,7,11,15]", index: 0 },
{ label: "target", value: "9", index: 1 },
],
},
expectedOutput: "[0,1]",
},
{
data: {
create: [
{ label: "nums", value: "[3,2,4]", index: 0 },
{ label: "target", value: "6", index: 1 },
],
},
expectedOutput: "[1,2]",
},
{
data: {
create: [
{ label: "nums", value: "[3,3]", index: 0 },
{ label: "target", value: "6", index: 1 },
],
},
expectedOutput: "[0,1]",
},
],
},
},
{
displayId: 1001,
title: "两数相加",
description: `#### 2. 两数相加
---
  ****   ****    ****  
0 0 
** 1**
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/01/02/addtwonumber1.jpg)
\`\`\`
l1 = [2,4,3], l2 = [5,6,4]
[7,0,8]
342 + 465 = 807.
\`\`\`
** 2**
\`\`\`
l1 = [0], l2 = [0]
[0]
\`\`\`
** 3**
\`\`\`
l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
[8,9,9,9,0,0,0,1]
\`\`\`
****
* \`[1, 100]\`
* \`0 <= Node.val <= 9\`
*
---
\`\`\`C++
\`\`\``,
solution: `## 方法一:模拟
###
n1,n2 carry n1+n2+carry (n1+n2+carry)mod10
10
0
carry>0 carry
###
\`\`\`c showLineNumbers
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode* dummyHead = malloc(sizeof(struct ListNode));
dummyHead->val = 0;
dummyHead->next = NULL;
struct ListNode* curr = dummyHead;
int carry = 0;
while (l1 != NULL || l2 != NULL || carry != 0) {
int x = (l1 != NULL) ? l1->val : 0;
int y = (l2 != NULL) ? l2->val : 0;
int sum = carry + x + y;
carry = sum / 10;
curr->next = malloc(sizeof(struct ListNode));
curr->next->val = sum % 10;
curr->next->next = NULL;
curr = curr->next;
if (l1 != NULL) l1 = l1->next;
if (l2 != NULL) l2 = l2->next;
}
struct ListNode* result = dummyHead->next;
free(dummyHead); // Free the memory allocated for dummyHead
return result;
}
\`\`\`
###
- **:** $O(max(m,n))$
$m$ $n$ $O(1)$
- **:** $O(1)$
`,
difficulty: "MEDIUM",
published: true,
templates: {
create: [
{
language: "c",
template: `
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Definition for singly-linked list.
struct ListNode {
int val;
struct ListNode *next;
};
// 创建链表
struct ListNode* createList(char *line) {
struct ListNode dummy;
struct ListNode *tail = &dummy;
dummy.next = NULL;
line[strcspn(line, "\\n")] = 0;
char *p = line;
while (*p && (*p == '[' || *p == ' ' || *p == ']')) p++;
char *token = strtok(p, ",");
while (token) {
struct ListNode *node = malloc(sizeof(struct ListNode));
node->val = atoi(token);
node->next = NULL;
tail->next = node;
tail = node;
token = strtok(NULL, ",");
}
return dummy.next;
}
// 打印链表
void printList(struct ListNode* head) {
printf("[");
while (head) {
printf("%d", head->val);
if (head->next) printf(",");
head = head->next;
}
printf("]\\n");
}
// 释放链表内存
void freeList(struct ListNode* head) {
while (head) {
struct ListNode* temp = head;
head = head->next;
free(temp);
}
}
// 主函数
int main() {
char line[1024];
while (fgets(line, sizeof(line), stdin)) {
struct ListNode* l1 = createList(line);
if (!fgets(line, sizeof(line), stdin)) break;
struct ListNode* l2 = createList(line);
struct ListNode* result = addTwoNumbers(l1, l2);
printList(result);
freeList(l1);
freeList(l2);
freeList(result);
}
return 0;
}
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
return NULL; // 在这里填充你的算法逻辑
}
`,
},
{
language: "cpp",
template: `
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
// 声明 Solution 类
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2);
};
// 输入字符串 -> 链表
ListNode* createList(const string& line) {
ListNode dummy;
ListNode* tail = &dummy;
dummy.next = nullptr;
string nums = line;
nums.erase(remove(nums.begin(), nums.end(), '['), nums.end());
nums.erase(remove(nums.begin(), nums.end(), ']'), nums.end());
stringstream ss(nums);
string token;
while (getline(ss, token, ',')) {
if (!token.empty()) {
int val = stoi(token);
tail->next = new ListNode(val);
tail = tail->next;
}
}
return dummy.next;
}
// 打印链表
void printList(ListNode* head) {
cout << "[";
while (head) {
cout << head->val;
if (head->next) cout << ",";
head = head->next;
}
cout << "]" << endl;
}
// 释放内存
void freeList(ListNode* head) {
while (head) {
ListNode* tmp = head;
head = head->next;
delete tmp;
}
}
// 主函数
int main() {
string line;
while (getline(cin, line)) {
ListNode* l1 = createList(line);
if (!getline(cin, line)) break;
ListNode* l2 = createList(line);
Solution sol;
ListNode* res = sol.addTwoNumbers(l1, l2);
printList(res);
freeList(l1);
freeList(l2);
freeList(res);
}
return 0;
}
ListNode* Solution::addTwoNumbers(ListNode* l1, ListNode* l2) {
return nullptr; // 在这里填充你的算法逻辑
}
`,
},
],
},
testcases: {
create: [
{
data: {
create: [
{ label: "l1", value: "[2,4,3]", index: 0 },
{ label: "l2", value: "[5,6,4]", index: 1 },
],
},
expectedOutput: "[7,0,8]",
},
{
data: {
create: [
{ label: "l1", value: "[0]", index: 0 },
{ label: "l2", value: "[0]", index: 1 },
],
},
expectedOutput: "[0]",
},
{
data: {
create: [
{ label: "l1", value: "[9,9,9,9,9,9,9]", index: 0 },
{ label: "l2", value: "[9,9,9,9]", index: 1 },
],
},
expectedOutput: "[8,9,9,9,0,0,0,1]",
},
],
},
},
],
},
},
{
name: "fly6516",
email: "fly6516@outlook.com",
password: "$2b$10$SD1T/dYvKTArGdTmf8ERxuBKIONxY01/wSboRNaNsHnKZzDhps/0u",
role: "ADMIN",
problems: {
create: [
{
displayId: 1002,
title: "寻找两个正序数组的中位数",
description: `#### 4. 寻找两个正序数组的中位数
---
\`m\`\`n\` 的正序(从小到大)数组 \`nums1\` 和 \`nums2\`。请你找出并返回这两个正序数组的 **中位数** 。
\`O(log (m+n))\`
** 1**
\`\`\`
nums1 = [1,3], nums2 = [2]
2.00000
= [1,2,3] 2
\`\`\`
** 2**
\`\`\`
nums1 = [1,2], nums2 = [3,4]
2.50000
= [1,2,3,4] (2 + 3) / 2 = 2.5
\`\`\`
****
* \`nums1.length == m\`
* \`nums2.length == n\`
* \`0 <= m <= 1000\`
* \`0 <= n <= 1000\`
* \`1 <= m + n <= 2000\`
* \`-10^6 <= nums1[i], nums2[i] <= 10^6\`
---
\`\`\`C++
\`\`\``,
solution: `## 方法一:二分查找
### Intuition
- 使
- 0
# $m$ $n$
$O(m+n)$ $O(m+n)$ $O(1)$ $O(m+n)$
$O(log(m+n))$ $log$
$m+n$ $(m+n)/2$ $m+n$ $(m+n)/2$ $(m+n)/2+1$ $k$ $k$ $(m+n)/2$ $(m+n)/2+1$
$A$ $B$ $k$ $A[k/21]$ $B[k/21]$ $/$ $A[k/21]$ $B[k/21]$ $A[0..k/22]$ $B[0..k/22]$ $k/21$ $A[k/21]$ $B[k/21]$ $(k/21)+(k/21)k2$ $k$
![](https://assets.leetcode-cn.com/solution-static/4/4_fig1.png
- $A[k/21]<B[k/21]$则比 $A[k/21]$ 小的数最多只有 $A$ 的前 $k/21$ 个数和 $B$ 的前 $k/21$ 个数即比 $A[k/21]$ 小的数最多只有 $k2$ 因此 $A[k/21]$ 不可能是第 $k$ 个数$A[0]$ $A[k/21]$ 也都不可能是第 $k$ 个数可以全部排除
- $A[k/21]>B[k/21]$ $B[0]$ $B[k/21]$
- $A[k/21]=B[k/21]$
$A[k/21] $B[k/21]$ $k/2$ $k$ $k$ $k$
- $A[k/21]$ $B[k/21]$ $k$ $k$ $k/2$
- $k$
- $k=1$
\`\`\`math
A: 1 3 4 9
B: 1 2 3 4 5 6 7 8 9
\`\`\`
4 9 13 7 k=7
k/21=2 A[2] B[2]
\`\`\`math
A: 1 3 4 9
B: 1 2 3 4 5 6 7 8 9
\`\`\`
A[2]>B[2] B[0] B[2] B offset 3 k k=kk/2=4
k/21=1 A[1] B[4]
\`\`\`math
A: 1 3 4 9
B: [1 2 3] 4 5 6 7 8 9
\`\`\`
A[1]<B[4]因此排除 A[0] A[1]即数组 A 的下标偏移变为 2同时更新 k 的值k=kk/2=2
k/21=0 A[2] B[3]
\`\`\`math
A: [1 3] 4 9
B: [1 2 3] 4 5 6 7 8 9
\`\`\`
A[2]=B[3] A A[2] A 3 k k=kk/2=1
k 1 k A[3]>B[3] k B[3]=4
\`\`\`math
A: [1 3 4] 9
B: [1 2 3] 4 5 6 7 8 9
\`\`\`
###
\`\`\`c showLineNumbers
int solve(int* A, int aStart, int aEnd, int* B, int bStart, int bEnd, int k) {
// If the segment of on array is empty, it means we have passed all
// its element, just return the corresponding element in the other array.
if (aEnd < aStart) {
return B[k - aStart];
}
if (bEnd < bStart) {
return A[k - bStart];
}
// Get the middle indexes and middle values of A and B.
int aIndex = (aStart + aEnd) / 2, bIndex = (bStart + bEnd) / 2;
int aValue = A[aIndex], bValue = B[bIndex];
// If k is in the right half of A + B, remove the smaller left half.
if (aIndex + bIndex < k) {
if (aValue > bValue) {
return solve(A, aStart, aEnd, B, bIndex + 1, bEnd, k);
} else {
return solve(A, aIndex + 1, aEnd, B, bStart, bEnd, k);
}
}
// Otherwise, remove the larger right half.
else {
if (aValue > bValue) {
return solve(A, aStart, aIndex - 1, B, bStart, bEnd, k);
} else {
return solve(A, aStart, aEnd, B, bStart, bIndex - 1, k);
}
}
}
double findMedianSortedArrays(int* A, int na, int* B, int nb) {
int n = na + nb;
if (n % 2 == 1) {
return solve(A, 0, na - 1, B, 0, nb - 1, n / 2);
} else {
return (solve(A, 0, na - 1, B, 0, nb - 1, n / 2) +
solve(A, 0, na - 1, B, 0, nb - 1, n / 2 - 1)) /
2.0;
}
}
\`\`\`
###
Let $m$ be the size of array \`nums1\` and $n$ be the size of array \`nums2\`.
- **:** $O(log(m+n))$
- m n nums 1 nums 2 $k=(m+n)/2$ $k=(m+n)/2+1$ $O(log(m+n))$
- **:** $O(1)$
`,
difficulty: "HARD",
published: true,
templates: {
create: [
{
language: "c",
template: `
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 解析输入数组
int *parseIntArray(char *line, int *len) {
line[strcspn(line, "\\n")] = 0; // 移除换行符
char *p = line;
while (*p && (*p == '[' || *p == ' ' || *p == ']')) p++; // 跳过空格和括号
int capacity = 10;
int *arr = malloc(capacity * sizeof(int)); // 初始分配空间
*len = 0;
char *token = strtok(p, ","); // 分割输入为逗号分隔的整数
while (token) {
if (*len >= capacity) { // 扩展数组大小
capacity *= 2;
arr = realloc(arr, capacity * sizeof(int));
}
arr[(*len)++] = atoi(token); // 存储整数
token = strtok(NULL, ",");
}
return arr;
}
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size);
int main() {
char line[1024];
while (fgets(line, sizeof(line), stdin)) { // 读取第一行
int len1;
int *nums1 = parseIntArray(line, &len1); // 解析数组1
if (!fgets(line, sizeof(line), stdin)) break; // 如果第二行不存在,退出
int len2;
int *nums2 = parseIntArray(line, &len2); // 解析数组2
double result = findMedianSortedArrays(nums1, len1, nums2, len2); // 计算中位数
printf("%.5f\\n", result); // 输出中位数保留5位小数
free(nums1); // 释放内存
free(nums2); // 释放内存
}
return 0;
}
// 寻找中位数函数
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
return 0.0; // 在这里填充你的算法逻辑
}
`,
},
{
language: "cpp",
template: `
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2);
};
// 解析输入为整数数组
vector<int> parseIntArray(const string& line) {
string trimmed = line;
trimmed.erase(remove(trimmed.begin(), trimmed.end(), '['), trimmed.end());
trimmed.erase(remove(trimmed.begin(), trimmed.end(), ']'), trimmed.end());
vector<int> result;
stringstream ss(trimmed);
string token;
while (getline(ss, token, ',')) {
if (!token.empty()) {
result.push_back(stoi(token));
}
}
return result;
}
int main() {
string line;
while (getline(cin, line)) {
vector<int> nums1 = parseIntArray(line);
if (!getline(cin, line)) break;
vector<int> nums2 = parseIntArray(line);
Solution sol;
double result = sol.findMedianSortedArrays(nums1, nums2);
printf("%.5f\\n", result);
}
return 0;
}
double Solution::findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
return 0.0; // 临时返回值,待填充
}
`,
},
],
},
testcases: {
create: [
{
data: {
create: [
{ label: "nums1", value: "[1,3]", index: 0 },
{ label: "nums2", value: "[2]", index: 1 },
],
},
expectedOutput: "2.00000",
},
{
data: {
create: [
{ label: "nums1", value: "[1,2]", index: 0 },
{ label: "nums2", value: "[3,4]", index: 1 },
],
},
expectedOutput: "2.50000",
},
],
},
},
],
},
},
];
export async function main() {
for (const e of editorLanguageConfigData) {
await prisma.editorLanguageConfig.create({ data: e });
}
for (const u of userData) {
await prisma.user.create({ data: u });
}
}
main();