Skip to content

Add a New LeetCode Solution (C CMake)

Ragaad edited this page Oct 22, 2025 · 1 revision

Add a New LeetCode Solution (C++/CMake)

How to scaffold, implement, build, test, and run a new problem using this repo.

Jump to:


Prerequisites

Install once on macOS:

brew install cmake ninja

First configure (first time only):

cmake --preset default

Repo Layout

leetcodeExamples/
├─ 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

Step-by-Step: Add a Problem

Example uses ID 0121 and slug best-time-to-buy-and-sell-stock. Replace with any 4-digit ID and kebab-case slug.

  1. Create the scaffold

    ./scripts/new_problem.sh 0121 best-time-to-buy-and-sell-stock
  2. 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
    
  3. Build

    cmake --build --preset default
  4. 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
  5. (Optional) Run the example executable

    ./build/problems/0121-best-time-to-buy-and-sell-stock/0121_best_time_to_buy_and_sell_stock

Example: 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 & Test

# 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

Run the Executable

./build/problems/0121-best-time-to-buy-and-sell-stock/0121_best_time_to_buy_and_sell_stock

Commit & Push

git 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

Conventions & Tips

  • Folder name: problems/<4-digit-id>-<slug>/
  • Target name: folder name with hyphens → underscores (used by ctest -R).
  • Headers: include leetcode.hpp (which includes pch.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);

Troubleshooting

  • fatal error: 'bits/stdc++.h' file not found
    Use the provided pch.hpp (already included by leetcode.hpp). Do not include <bits/stdc++.h> on macOS.

  • Tests aren’t discovered
    Ensure the problem’s CMakeLists.txt contains the *_tests target and:

    include(GoogleTest)
    gtest_discover_tests(<your_target>_tests)
  • ctest -R doesn’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