-
Notifications
You must be signed in to change notification settings - Fork 0
Add a New LeetCode Solution (C CMake)
How to scaffold, implement, build, test, and run a new problem using this repo.
Jump to:
- Prerequisites
- Repo Layout
- Step-by-Step: Add a Problem
- Example: 0121 — Best Time to Buy and Sell Stock
- Build & Test
- Run the Executable
- Commit & Push
- Conventions & Tips
- Troubleshooting
Install once on macOS:
brew install cmake ninjaFirst configure (first time only):
cmake --preset defaultleetcodeExamples/
├─ CMakeLists.txt
├─ CMakePresets.json
├─ include/
│ ├─ pch.hpp # portable replacement for <bits/stdc++.h>
│ └─ leetcode.hpp # ListNode, TreeNode, helpers
├─ problems/
│ └─ <id>-<slug>/
│ ├─ CMakeLists.txt
│ ├─ main.cpp # solution + tiny demo main
│ └─ test.cpp # GoogleTest unit tests
└─ scripts/
└─ new_problem.sh # scaffolds a new problem folder
Example uses ID
0121and slugbest-time-to-buy-and-sell-stock. Replace with any 4-digit ID and kebab-case slug.
-
Create the scaffold
./scripts/new_problem.sh 0121 best-time-to-buy-and-sell-stock
-
Implement your solution in:
problems/0121-best-time-to-buy-and-sell-stock/main.cpp problems/0121-best-time-to-buy-and-sell-stock/test.cpp -
Build
cmake --build --preset default
-
Run tests
ctest --preset default --output-on-failure # or only this problem's tests (regex): ctest --preset default -R 0121_best_time_to_buy_and_sell_stock --output-on-failure -
(Optional) Run the example executable
./build/problems/0121-best-time-to-buy-and-sell-stock/0121_best_time_to_buy_and_sell_stock
problems/0121-best-time-to-buy-and-sell-stock/main.cpp
#include "leetcode.hpp"
using namespace std;
class Solution {
public:
int maxProfit(const vector<int>& prices) {
int minP = INT_MAX, best = 0;
for (int p : prices) {
minP = min(minP, p);
best = max(best, p - minP);
}
return best;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
Solution s;
cout << s.maxProfit({7,1,5,3,6,4}) << "\n"; // 5
return 0;
}problems/0121-best-time-to-buy-and-sell-stock/test.cpp
#include "leetcode.hpp"
#include <gtest/gtest.h>
using namespace std;
class Solution {
public:
int maxProfit(const vector<int>& prices) {
int minP = INT_MAX, best = 0;
for (int p : prices) {
minP = min(minP, p);
best = max(best, p - minP);
}
return best;
}
};
TEST(MaxProfit, Example1) {
Solution s;
EXPECT_EQ(s.maxProfit({7,1,5,3,6,4}), 5);
}
TEST(MaxProfit, MonotoneDecreasing) {
Solution s;
EXPECT_EQ(s.maxProfit({7,6,4,3,1}), 0);
}
TEST(MaxProfit, SingleDay) {
Solution s;
EXPECT_EQ(s.maxProfit({5}), 0);
}# build everything
cmake --build --preset default
# run all tests
ctest --preset default --output-on-failure
# run just this problem's tests (regex)
ctest --preset default -R 0121_best_time_to_buy_and_sell_stock --output-on-failure./build/problems/0121-best-time-to-buy-and-sell-stock/0121_best_time_to_buy_and_sell_stockgit add problems/0121-best-time-to-buy-and-sell-stock
git commit -m "Add 0121 best-time-to-buy-and-sell-stock (solution + tests)"
git push-
Folder name:
problems/<4-digit-id>-<slug>/ -
Target name: folder name with hyphens → underscores (used by
ctest -R). -
Headers: include
leetcode.hpp(which includespch.hpp) — portable on macOS; avoid<bits/stdc++.h>. -
Helpers (from
leetcode.hpp):// Linked list auto* head = list_from_vec({1,2,3}); EXPECT_EQ(vec_from_list(head), (vector<int>{1,2,3})); // Quick BST insert TreeNode* root = nullptr; for (int x : {5,3,7}) root = bst_insert(root, x);
-
fatal error: 'bits/stdc++.h' file not found
Use the providedpch.hpp(already included byleetcode.hpp). Do not include<bits/stdc++.h>on macOS. -
Tests aren’t discovered
Ensure the problem’sCMakeLists.txtcontains the*_teststarget and:include(GoogleTest) gtest_discover_tests(<your_target>_tests) -
ctest -Rdoesn’t match
Use a broader substring (e.g.,-R 0121_best_time) or run all tests to see names.
Add another problem:
./scripts/new_problem.sh 0002 add-two-numbers
cmake --build --preset default
ctest --preset default -R 0002_add_two_numbers --output-on-failure