Repository navigation
Expand file tree
/
Copy pathsetup_leetcode.sh
More file actions
executable file
·320 lines (245 loc) · 8.65 KB
/
Copy pathsetup_leetcode.sh
File metadata and controls
executable file
·320 lines (245 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/bin/bash
# Create directory structure
mkdir -p LeetcodeNotes/src
mkdir -p LeetcodeNotes/include
mkdir -p LeetcodeNotes/build
mkdir -p LeetcodeNotes/docs
# Create the main driver file
cat > LeetcodeNotes/src/main.cpp << 'EOF'
#include <iostream>
#include <string>
#include "solution.h"
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cout << "Usage: " << argv[0] << " [problem_name]" << std::endl;
return 1;
}
std::string problem_name = argv[1];
std::cout << "Running solution for problem: " << problem_name << std::endl;
// Execute the solution
Solution solution;
solution.run();
return 0;
}
EOF
# Create a base solution header
cat > LeetcodeNotes/include/solution.h << 'EOF'
#ifndef SOLUTION_H
#define SOLUTION_H
class Solution {
public:
virtual void run() = 0;
virtual ~Solution() {}
};
#endif // SOLUTION_H
EOF
# Create a template for new problems
cat > LeetcodeNotes/template.cpp << 'EOF'
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>
#include "solution.h"
// Problem: [PROBLEM_NAME]
// Description: [PROBLEM_DESCRIPTION]
class [PROBLEM_NAME]Solution : public Solution {
public:
// Your solution implementation here
void run() override {
// Test cases
std::cout << "Running test cases for [PROBLEM_NAME]..." << std::endl;
// Example test case
// auto result = yourSolutionMethod(params);
// std::cout << "Result: " << result << std::endl;
}
};
// If this file is being compiled as the main solution
#ifdef CURRENT_SOLUTION
Solution* createSolution() {
return new [PROBLEM_NAME]Solution();
}
#endif
EOF
# Create a template for problem documentation
cat > LeetcodeNotes/template.md << 'EOF'
# [PROBLEM_NAME]
## Problem Description
[PROBLEM_DESCRIPTION]
## Approach
[DESCRIBE_YOUR_APPROACH]
## Time Complexity
- Time Complexity: [TIME_COMPLEXITY]
- Space Complexity: [SPACE_COMPLEXITY]
## Notes
[ANY_ADDITIONAL_NOTES]
EOF
# Create Makefile
cat > LeetcodeNotes/Makefile << 'EOF'
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -I./include
SRC_DIR = src
BUILD_DIR = build
PROBLEM_NAME ?= TwoSum # Default problem name
# Find the problem source file
PROBLEM_SRC = $(shell find $(SRC_DIR) -name "$(PROBLEM_NAME).cpp" -o -name "$(PROBLEM_NAME)_*.cpp" | head -1)
all: clean $(BUILD_DIR)/$(PROBLEM_NAME)
$(BUILD_DIR)/$(PROBLEM_NAME): $(PROBLEM_SRC) $(SRC_DIR)/main.cpp
@mkdir -p $(BUILD_DIR)
$(CXX) $(CXXFLAGS) -DCURRENT_SOLUTION $(SRC_DIR)/main.cpp $(PROBLEM_SRC) -o $@
run: $(BUILD_DIR)/$(PROBLEM_NAME)
./$(BUILD_DIR)/$(PROBLEM_NAME) $(PROBLEM_NAME)
new:
@if [ -z "$(PROBLEM_NAME)" ]; then \
echo "Please provide a problem name: make new PROBLEM_NAME=YourProblemName"; \
exit 1; \
fi
@if [ -f "$(SRC_DIR)/$(PROBLEM_NAME).cpp" ]; then \
echo "Problem already exists!"; \
exit 1; \
fi
@mkdir -p $(SRC_DIR)
@sed "s/\[PROBLEM_NAME\]/$(PROBLEM_NAME)/g" template.cpp > $(SRC_DIR)/$(PROBLEM_NAME).cpp
@sed "s/\[PROBLEM_NAME\]/$(PROBLEM_NAME)/g" template.md > docs/$(PROBLEM_NAME).md
@echo "Created new problem: $(PROBLEM_NAME)"
@echo "Source file: $(SRC_DIR)/$(PROBLEM_NAME).cpp"
@echo "Documentation: docs/$(PROBLEM_NAME).md"
list:
@echo "Available problems:"
@find $(SRC_DIR) -name "*.cpp" | grep -v "main.cpp" | sed 's/.*\/\(.*\)\.cpp/\1/'
clean:
rm -rf $(BUILD_DIR)/*
.PHONY: all run new list clean
EOF
# Create a utility script to make new problems more easily
cat > LeetcodeNotes/new_problem.sh << 'EOF'
#!/bin/bash
if [ $# -lt 1 ]; then
echo "Usage: $0 <problem_name> [problem_description]"
exit 1
fi
PROBLEM_NAME=$1
PROBLEM_DESCRIPTION=${2:-"Add your problem description here"}
make new PROBLEM_NAME="$PROBLEM_NAME"
# Optionally update the description
if [ "$PROBLEM_DESCRIPTION" != "Add your problem description here" ]; then
sed -i "s/\[PROBLEM_DESCRIPTION\]/$PROBLEM_DESCRIPTION/g" docs/$PROBLEM_NAME.md
sed -i "s/\[PROBLEM_DESCRIPTION\]/$PROBLEM_DESCRIPTION/g" src/$PROBLEM_NAME.cpp
fi
echo "You can now edit src/$PROBLEM_NAME.cpp to implement your solution"
echo "Document your approach in docs/$PROBLEM_NAME.md"
echo "To build and run: make run PROBLEM_NAME=$PROBLEM_NAME"
EOF
chmod +x LeetcodeNotes/new_problem.sh
# Create a sample problem solution
cat > LeetcodeNotes/src/TwoSum.cpp << 'EOF'
#include <iostream>
#include <vector>
#include <unordered_map>
#include "solution.h"
// Problem: TwoSum
// Description: Given an array of integers nums and an integer target,
// return indices of the two numbers such that they add up to target.
class TwoSumSolution : public Solution {
public:
std::vector<int> twoSum(std::vector<int>& nums, int target) {
std::unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++) {
int complement = target - nums[i];
if (map.find(complement) != map.end()) {
return {map[complement], i};
}
map[nums[i]] = i;
}
return {}; // No solution found
}
void run() override {
std::cout << "Running TwoSum solution..." << std::endl;
// Test cases
std::vector<int> nums1 = {2, 7, 11, 15};
int target1 = 9;
auto result1 = twoSum(nums1, target1);
std::cout << "Input: nums = [2,7,11,15], target = 9" << std::endl;
std::cout << "Output: [" << result1[0] << "," << result1[1] << "]" << std::endl;
std::vector<int> nums2 = {3, 2, 4};
int target2 = 6;
auto result2 = twoSum(nums2, target2);
std::cout << "Input: nums = [3,2,4], target = 6" << std::endl;
std::cout << "Output: [" << result2[0] << "," << result2[1] << "]" << std::endl;
}
};
// If this file is being compiled as the main solution
#ifdef CURRENT_SOLUTION
Solution* createSolution() {
return new TwoSumSolution();
}
#endif
EOF
# Create a sample problem documentation
cat > LeetcodeNotes/docs/TwoSum.md << 'EOF'
# TwoSum
## 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.
## Approach
1. Use a hash map to store each number and its index as we iterate through the array.
2. For each number, check if its complement (target - current number) exists in the hash map.
3. If the complement exists, return the current index and the complement's index.
4. Otherwise, add the current number and its index to the hash map and continue.
## Time Complexity
- Time Complexity: O(n) where n is the length of the array. We traverse the array once.
- Space Complexity: O(n) for the hash map that stores at most n elements.
## Notes
This is an optimal solution as it achieves O(n) time complexity with a single pass through the array.
EOF
# Create a README.md file
cat > LeetcodeNotes/README.md << 'EOF'
# LeetCode Solutions Project
A C++ project structure for organizing and running LeetCode problem solutions.
## Project Structure
```
LeetcodeNotes/
├── include/ # Header files
├── src/ # Source files for problems
├── build/ # Compiled binaries
├── docs/ # Documentation for problems
├── Makefile # Build system
├── template.cpp # Template for new problems
├── template.md # Template for documentation
└── new_problem.sh # Script to create new problems
```
## Usage
### Create a new problem
```bash
./new_problem.sh ProblemName "Short description of the problem"
```
This will:
1. Create a new source file in `src/ProblemName.cpp`
2. Create a documentation file in `docs/ProblemName.md`
### Implement your solution
Edit the generated source file to implement your solution for the problem.
### Document your approach
Edit the documentation file to describe your approach, time complexity, and any other notes.
### Build and run
```bash
make run PROBLEM_NAME=ProblemName
```
### List available problems
```bash
make list
```
## Example
The project comes with a sample "TwoSum" problem implementation.
To run it:
```bash
make run PROBLEM_NAME=TwoSum
```
EOF
echo "LeetCode solutions project structure created in the 'LeetcodeNotes' directory!"
echo "To get started:"
echo " 1. cd LeetcodeNotes"
echo " 2. ./new_problem.sh YourProblemName \"Problem description\""
echo " 3. Edit src/YourProblemName.cpp to implement your solution"
echo " 4. Document your approach in docs/YourProblemName.md"
echo " 5. Run: make run PROBLEM_NAME=YourProblemName"