refactor(config): reorganize language configuration and remove deprecated files

This commit is contained in:
cfngc4594 2025-02-25 14:39:41 +08:00
parent 4022e8559b
commit d51c324ea9
9 changed files with 249 additions and 219 deletions

View File

@ -0,0 +1,3 @@
import { SUPPORTED_LANGUAGES, SupportedLanguage } from "@/constants/language";
export const DEFAULT_EDITOR_LANGUAGE: SupportedLanguage = SUPPORTED_LANGUAGES[0].id;

View File

@ -1,6 +1,6 @@
import { SupportedLanguage } from "@/constants/language";
export const DEFAULT_PATH: Record<SupportedLanguage, string> = {
export const DEFAULT_EDITOR_PATH: Record<SupportedLanguage, string> = {
c: "file:///main.c",
cpp: "file:///main.cpp",
};

View File

@ -0,0 +1,16 @@
import { SupportedLanguage } from "@/constants/language";
export const DEFAULT_EDITOR_VALUE: Record<SupportedLanguage, string> = {
c: `/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
}`,
cpp: `class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
}
};`,
};

View File

@ -1,3 +0,0 @@
import { SUPPORTED_LANGUAGES, SupportedLanguage } from "@/constants/language";
export const DEFAULT_LANGUAGE: SupportedLanguage = SUPPORTED_LANGUAGES[0].id;

View File

@ -1,143 +0,0 @@
export const DEFAULT_PROBLEM = `
# Two Sum
## Problem Description
Given an array of integers \`nums\` and an integer \`target\`, return indices of the two numbers such that they add up to \`target\`.
You may assume that each input would have **exactly one solution**, and you may not use the same element twice.
You can return the answer in any order.
### Example 1
\`\`\`sh
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
\`\`\`
### Example 2
\`\`\`sh
Input: nums = [3,2,4], target = 6
Output: [1,2]
Explanation: Because nums[1] + nums[2] == 6, we return [1, 2].
\`\`\`
### Example 3
\`\`\`sh
Input: nums = [3,3], target = 6
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 6, we return [0, 1].
\`\`\`
## Solution Approach
Use a hash table to store each number and its corresponding index. Then, iterate through the array, and for each number, check if \`target - num\` exists in the hash table.
## C Language Code
\`\`\`c showLineNumbers title="Two Sum" caption="Example Code" {4-8,40-43} {22} {29,32,33} /target - nums/
#include <stdio.h>
#include <stdlib.h>
// Define Hash Table structure
typedef struct {
int key;
int value;
} HashTable;
// Hash Table insert function
void insert(HashTable *table, int key, int value) {
// Simplified handling, assuming no collisions
table[key].key = key;
table[key].value = value;
}
// Hash Table search function
int search(HashTable *table, int key) {
if (table[key].key == key) {
return table[key].value;
}
return -1; // Not found
}
// Two Sum function
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
HashTable *table = (HashTable *)malloc(sizeof(HashTable) * 1024); // Assume a small range of array elements
*returnSize = 2;
int *result = (int *)malloc(sizeof(int) * (*returnSize));
// Build the Hash Table
for (int i = 0; i < numsSize; i++) {
insert(table, nums[i], i);
}
// Find the two numbers that meet the criteria
for (int i = 0; i < numsSize; i++) {
int complement = target - nums[i];
int index = search(table, complement);
if (index != -1 && index != i) { // Found two numbers that meet the criteria
result[0] = i;
result[1] = index;
break;
}
}
free(table);
return result;
}
int main() {
int nums[] = {2, 7, 11, 15};
int numsSize = sizeof(nums) // sizeof(nums[0]);
int target = 9;
int returnSize;
int *result = twoSum(nums, numsSize, target, &returnSize);
printf("Result: [%d, %d]\\n", result[0], result[1]);
free(result);
return 0;
}
\`\`\`
## Code Comment Highlighting
- Line 29: \`if (index != -1 && index != i)\`, highlighted to emphasize that the two numbers found cannot be the same number.
- Line 32: \`result[0] = i;\`, highlighted to indicate that the index of the first number is stored in the result array.
- Line 33: \`result[1] = index;\`, highlighted to indicate that the index of the second number is stored in the result array.
## Task List
- [x] Complete problem description
- [ ] Complete solution approach
- [x] Complete C language code
- [ ] Add code comment highlighting
- [x] Add example code and results
## Table
| **Language** | **LSP Server** | **Port** |
|----------------|------------------|------------|
| \`C\` | \`clangd\` | \`4594\` |
| \`C++\` | \`clangd\` | \`4595\` |
## ANSI Highlighting
\`\`\`ansi
HelloWorld.java:3: error: ';' expected
System.out.println("Hello, World!")
 ^
1 error
\`\`\`
## Inline Code
- Regular inline code: \`int x = 10;\`
- Highlighted inline code: \`int x = 10;{:c}\`
`

View File

@ -0,0 +1,49 @@
export const DEFAULT_PROBLEM_DESCRIPTION = `
# Two Sum
Given an array of integers \`nums\` and an integer \`target\`, return *indices* of the **two numbers** such that they add up to \`target\`.
You **must not** use the same element twice, and you are guaranteed to have **exactly one solution** for each input.
The order of the indices you return does not matter.
## Examples
### Example 1
\`\`\`bash
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
\`\`\`
### Example 2
\`\`\`bash
Input: nums = [3,2,4], target = 6
Output: [1,2]
Explanation: Because nums[1] + nums[2] == 6, we return [1, 2].
\`\`\`
### Example 3
\`\`\`bash
Input: nums = [3,3], target = 6
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 6, we return [0, 1].
\`\`\`
## Constraints
* \`2 <= nums.length <= 10^4\`
* \`-10^9 <= nums[i] <= 10^9\`
* \`-10^9 <= target <= 10^9\`
* **It is guaranteed that only one valid answer exists.**
**Follow-up:** Can you devise an algorithm with a time complexity less than \`O(n^2)\`?
`;

View File

@ -0,0 +1,180 @@
export const DEFAULT_PROBLEM_SOLUTION = `
# Solution Article: Two Sum Problem
This article explores different approaches to solve the "Two Sum" problem, ranging from a straightforward brute-force method to more efficient hash table based solutions.
## Approach 1: Brute Force
### Intuition
The most intuitive approach is to check every possible pair of numbers in the input array \`nums\`. For each number, we iterate through the rest of the array to find its complement that sums up to the \`target\`.
### Algorithm
1. Iterate through the \`nums\` array with index \`i\` from 0 to \`numsSize - 1\`.
2. For each element \`nums[i]\`, iterate through the rest of the array with index \`j\` from \`i + 1\` to \`numsSize - 1\`.
3. Check if the sum of \`nums[i] + nums[j]\` equals the \`target\`.
4. If the sum equals the \`target\`, return the indices \`[i, j]\`.
### Implementation
\`\`\`c showLineNumbers title="Brute Force Implementation in C" caption="Simple nested loop approach." {2-3} {8,14} /target - nums[i]/
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
for (int i = 0; i < numsSize; i++) {
for (int j = i + 1; j < numsSize; j++) {
if (nums[j] == target - nums[i]) {
int* result = malloc(sizeof(int) * 2);
result[0] = i;
result[1] = j;
*returnSize = 2;
return result;
}
}
}
// Return an empty array if no solution is found
*returnSize = 0;
return malloc(sizeof(int) * 0);
}
\`\`\`
### Complexity Analysis
- **Time complexity: O(n^2)**. The nested loops result in a quadratic time complexity because, in the worst case, we compare each number with every other number in the array.
- **Space complexity: O(1)**. The algorithm uses a constant amount of extra space, regardless of the input size.
---
## Approach 2: Two-pass Hash Table
### Intuition
To optimize the runtime, we can use a hash table to reduce the lookup time for the complement. Instead of iterating through the rest of the array to find the complement, we can check for its existence in the hash table in near-constant time on average.
### Algorithm
1. Create an empty hash table.
2. **First pass:** Iterate through the \`nums\` array and insert each element's value as the key and its index as the value into the hash table.
3. **Second pass:** Iterate through the \`nums\` array again. For each element \`nums[i]\`, calculate its complement \`complement = target - nums[i]\`.
4. Check if the \`complement\` exists as a key in the hash table.
5. If the \`complement\` exists and its index in the hash table is not equal to \`i\` (to avoid using the same element twice), return the indices \`[i, index of complement from hash table]\`.
### Implementation
\`\`\`c showLineNumbers title="Two-pass Hash Table Implementation in C" caption="Using a hash table for faster lookups." {4-8} {22,27,35} /hashTable/ /HASH_ADD_INT/ /HASH_FIND_INT/ /HASH_DEL/ /HASH_ITER/
#include "uthash.h" // Assuming uthash.h is included for hash table implementation
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
struct hashTable {
int key;
int value;
UT_hash_handle hh;
} *hashTable = NULL, *item, *tmpItem;
// First pass: Populate hash table
for (int i = 0; i < numsSize; i++) {
item = malloc(sizeof(struct hashTable));
item->key = nums[i];
item->value = i;
HASH_ADD_INT(hashTable, key, item);
}
// Second pass: Find complement
for (int i = 0; i < numsSize; i++) {
int complement = target - nums[i];
HASH_FIND_INT(hashTable, &complement, item);
if (item && item->value != i) { // Ensure not using the same element twice (though problem guarantees only one solution, and using same index is avoided by algorithm logic anyway)
int* result = malloc(sizeof(int) * 2);
result[0] = i;
result[1] = item->value;
*returnSize = 2;
HASH_ITER(hh, hashTable, item, tmpItem) { // Free hash table
HASH_DEL(hashTable, item);
free(item);
}
return result;
}
}
HASH_ITER(hh, hashTable, item, tmpItem) { // Free hash table if no solution found (though should not happen as per problem constraints)
HASH_DEL(hashTable, item);
free(item);
}
*returnSize = 0;
return malloc(sizeof(int) * 0);
}
\`\`\`
### Complexity Analysis
- **Time complexity: O(n)**. We iterate through the array twice. Hash table operations (insertion and lookup) take O(1) time on average.
- **Space complexity: O(n)**. We use a hash table to store at most \`n\` elements, resulting in linear space complexity.
---
## Approach 3: One-pass Hash Table
### Intuition
We can optimize the two-pass hash table approach into a single pass. While iterating through the array, for each number, we immediately check if its complement already exists in the hash table. If it does, we have found the pair. If not, we add the current number to the hash table for future lookups.
### Algorithm
1. Create an empty hash table.
2. Iterate through the \`nums\` array with index \`i\` from 0 to \`numsSize - 1\`.
3. For each element \`nums[i]\`, calculate its complement \`complement = target - nums[i]\`.
4. Check if the \`complement\` exists as a key in the hash table.
5. If the \`complement\` exists in the hash table, return the indices \`[index of complement from hash table, i]\`.
6. If the \`complement\` does not exist, add the current element \`nums[i]\` as the key and its index \`i\` as the value to the hash table.
### Implementation
\`\`\`c showLineNumbers title="One-pass Hash Table Implementation in C" caption="Efficient single-pass solution." {4-8} {15-16,18,28-29} /hashTable/ /HASH_ADD_INT/ /HASH_FIND_INT/ /HASH_CLEAR/
#include "uthash.h" // Assuming uthash.h is included for hash table implementation
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
struct hashTable {
int key;
int value;
UT_hash_handle hh;
} *hashTable = NULL, *item;
for (int i = 0; i < numsSize; i++) {
int complement = target - nums[i];
HASH_FIND_INT(hashTable, &complement, item);
if (item) {
int* result = malloc(sizeof(int) * 2);
result[0] = item->value; // Index of complement (which was added earlier)
result[1] = i; // Current index
*returnSize = 2;
HASH_CLEAR(hh, hashTable); // Free hash table
return result;
} else {
item = malloc(sizeof(struct hashTable));
item->key = nums[i];
item->value = i;
HASH_ADD_INT(hashTable, key, item);
}
}
*returnSize = 0;
HASH_CLEAR(hh, hashTable); // Free hash table if no solution found (though should not happen)
return malloc(0); // Allocate 0 bytes - indicates no solution
}
\`\`\`
### Complexity Analysis
- **Time complexity: O(n)**. We iterate through the array only once. Hash table operations are O(1) on average.
- **Space complexity: O(n)**. Similar to the two-pass approach, we use a hash table that can store up to \`n\` elements.
---
## Summary of Approaches
| Approach | Time Complexity | Space Complexity |
| ------------------- | --------------- | ---------------- |
| Brute Force | O(n^2) | O(1) |
| Two-pass Hash Table | O(n) | O(n) |
| One-pass Hash Table | O(n) | O(n) |
`;

View File

@ -1,72 +0,0 @@
import { SupportedLanguage } from "@/constants/language";
export const DEFAULT_VALUE: Record<SupportedLanguage, string> = {
c: `#include <stdio.h>
#define ARR_LEN 7
void qsort(int v[], int left, int right);
void printArr(int v[], int len);
int main() {
int i;
int v[ARR_LEN] = { 4, 3, 1, 7, 9, 6, 2 };
printArr(v, ARR_LEN);
qsort(v, 0, ARR_LEN - 1);
printArr(v, ARR_LEN);
return 0;
}
void qsort(int v[], int left, int right) {
int i, last;
void swap(int v[], int i, int j);
if (left >= right) {
return;
}
swap(v, left, (left + right) / 2);
last = left;
for (i = left + 1; i <= right; i++) {
if (v[i] < v[left]) {
swap(v, ++last, i);
}
}
swap(v, left, last);
qsort(v, left, last - 1);
qsort(v, last + 1, right);
}
void swap(int v[], int i, int j) {
int temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
void printArr(int v[], int len) {
int i;
for (i = 0; i < len; i++) {
printf("%d ", v[i]);
}
printf("\\n");
}`,
cpp: `// Working of implicit type-conversion
#include <iostream>
using namespace std;
int main() {
int num_int;
double num_double = 9.99;
// implicit conversion
// assigning a double value to an int variable
num_int = num_double;
cout << "num_int = " << num_int << endl;
cout << "num_double = " << num_double << endl;
return 0;
}`,
};