From b33e3df3296d1425051b3a64837fddec71040208 Mon Sep 17 00:00:00 2001 From: Ilya Volchenkov Date: Wed, 21 May 2025 14:26:13 +0300 Subject: [PATCH] Initial commit --- .gitignore | 1 + .vscode/c_cpp_properties.json | 19 ++++++++ 2/2.cpp | 65 +++++++++++++++++++++++++ 2558/2558.cpp | 43 +++++++++++++++++ 3341/3341.cpp | 71 +++++++++++++++++++++++++++ 838/838-1.cpp | 74 ++++++++++++++++++++++++++++ 838/838-2.cpp | 90 +++++++++++++++++++++++++++++++++++ 9/9.cpp | 46 ++++++++++++++++++ CMakeLists.txt | 15 ++++++ justfile | 9 ++++ main.cpp | 6 +++ 11 files changed, 439 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/c_cpp_properties.json create mode 100644 2/2.cpp create mode 100644 2558/2558.cpp create mode 100644 3341/3341.cpp create mode 100644 838/838-1.cpp create mode 100644 838/838-2.cpp create mode 100644 9/9.cpp create mode 100644 CMakeLists.txt create mode 100644 justfile create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c795b05 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..463e530 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,19 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "-std=c++20" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/2/2.cpp b/2/2.cpp new file mode 100644 index 0000000..3d385e2 --- /dev/null +++ b/2/2.cpp @@ -0,0 +1,65 @@ +#include + +namespace { + +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) {} +}; + +class Solution { + auto getNum(ListNode* l1, ListNode* l2) -> ListNode* { + ListNode *head, *prev, *curr; + + int v_ume = 0; + + while (l1 || l2) { + int a = 0, b = 0; + if (l1) { + a = l1->val; + l1 = l1->next; + } + if (l2) { + b = l2->val; + l2 = l2->next; + } + + int sum = a + b + v_ume; + + int x = sum % 10; + v_ume = sum / 10; + + curr = new ListNode(x); + if (!head) { + head = curr; + } else { + prev->next = curr; + } + prev = curr; + } + + if (v_ume != 0) { + curr = new ListNode(v_ume); + prev->next = curr; + } + + return head; + } + +public: + ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { + return getNum(l1, l2); + } +}; + +} + +#include + +TEST(P2, 1) { + Solution s; + auto res = s.addTwoNumbers(nullptr, nullptr); +} \ No newline at end of file diff --git a/2558/2558.cpp b/2558/2558.cpp new file mode 100644 index 0000000..ae4f358 --- /dev/null +++ b/2558/2558.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include + +void print(std::vector& gifts) { + for (auto gift : gifts) + std::cout << gift << ' '; + std::cout << "\n---\n"; +} + +namespace { + class Solution { + public: + long long pickGifts(std::vector& gifts, int k) { + std::sort(gifts.begin(), gifts.end()); + + for (int i = 0; i < k; ++i) { + auto value = std::sqrtl(gifts[gifts.size() - 1]); + if (value == 1) + continue; + auto it = std::upper_bound(gifts.begin(), gifts.end(), value); + auto idx = it - gifts.begin(); + std::shift_right(it, gifts.end(), 1); + gifts[idx] = value; + } + long long sum = 0; + for (auto gift : gifts) { + sum += gift; + } + return sum; + } + }; +} + +#include + +TEST(P2558, 1) { + std::vector gifts = {1,1,1,1}; + int k = 4; + Solution s; + auto res = s.pickGifts(gifts, k); +} \ No newline at end of file diff --git a/3341/3341.cpp b/3341/3341.cpp new file mode 100644 index 0000000..f469375 --- /dev/null +++ b/3341/3341.cpp @@ -0,0 +1,71 @@ +#include +#include +#include + +namespace { + struct Coord { + size_t x, y; + + explicit constexpr Coord(size_t x_, size_t y_) + : x(x_), y(y_) + {} + + + bool operator == (const Coord& other) const { + return std::tuple(x, y) == std::tuple(other.x, other.y); + } + }; + + struct Node : public Coord { + size_t g, h; + size_t f; + Node* parent; + + explicit constexpr Node(size_t x_, size_t y_) + : Coord(x_, y_) + {} + + explicit constexpr Node(const Coord& coord) + : Coord(coord.x, coord.y) + {} + + int operator < (const Node& other) const { + return f < other.f; + } + + bool operator == (const Node& other) const { + return Coord::operator==(other); + } + }; +} + + +class Solution3341 { +public: + int minTimeToReach(std::vector>& moveTime) { + constexpr Coord start{0, 0}; + const Coord end{moveTime.size(), moveTime[0].size()}; + + auto start_node = Node(start); + auto end_node = Node(end); + + std::priority_queue open_list; + open_list.push(start_node); + + std::set closed_set; + + while (open_list.size()) + { + Node current_node = open_list.top(); + open_list.pop(); + + if (current_node == end_node) { + std::vector path; + // while (current_node) + } + + } + + return 0; + } +}; diff --git a/838/838-1.cpp b/838/838-1.cpp new file mode 100644 index 0000000..e6c3202 --- /dev/null +++ b/838/838-1.cpp @@ -0,0 +1,74 @@ +#include +#include +#include + +namespace { + +enum Dominoes : char { + Dot = '.', + L = 'L', + R = 'R', +}; + +class Solution { + public: + std::string pushDominoes(std::string dominoes) { + bool has_actions = true; + + while (has_actions) { + size_t processed_dot = -1; + size_t actions_found = 0; + for (size_t i = 0; i < dominoes.size(); ++i) { + char d = dominoes[i]; + if ((d == L || d == R) && processed_dot != i) { + ++actions_found; + + bool left_dot = false; + bool right_dot = false; + + if (i > 0 && (dominoes[i - 1] == Dot && processed_dot != (i - 1))) + left_dot = true; + + if (i < (dominoes.size() - 1) && dominoes[i + 1] == Dot) + right_dot = true; + + if (d == L && left_dot) { + if (i > 1 && dominoes[i - 2] == R && processed_dot != (i - 2)) + --actions_found; + else { + dominoes[i - 1] = L; + processed_dot = (i - 1); + } + } + + else if (d == R && right_dot) { + if (i < (dominoes.size() - 2) && dominoes[i + 2] == L) + --actions_found; + else { + dominoes[i + 1] = R; + processed_dot = (i + 1); + } + } + + else + --actions_found; + } + } + if (!actions_found) + has_actions = false; + + std::cout << "step " << dominoes << '\n'; + } + return dominoes; + } + }; +} + +#include + +TEST(P838_1, 1) { + std::string input = ".L.R...LR..L.."; + Solution s; + auto result = s.pushDominoes(input); + EXPECT_EQ(result, std::string("LL.RR.LLRRLL..")); +} \ No newline at end of file diff --git a/838/838-2.cpp b/838/838-2.cpp new file mode 100644 index 0000000..25d276f --- /dev/null +++ b/838/838-2.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include + +namespace { + +enum Dominoes : char { + Dot = '.', + L = 'L', + R = 'R', +}; + +using Range = std::tuple; +using Ranges = std::vector; + +class Solution { + Ranges findRanges(const std::string& dominoes) { + Ranges ranges; + ranges.reserve(dominoes.size() / 2); + + size_t start = -1; + + for (size_t i = 0; i < dominoes.size(); ++i) { + char d = dominoes[i]; + + if (d == Dot && start == -1) { + start = i; + } else if (d != Dot && start != -1) { + ranges.emplace_back(start, i); + start = -1; + } + } + + if (start != -1) { + ranges.emplace_back(start, dominoes.size()); + } + + return ranges; + } + void processRange(const Range& range, std::string& dominoes) { + bool left_r = false; + bool right_l = false; + + const auto& [start, end] = range; + + if (start > 0 && dominoes[start - 1] == R) + left_r = true; + if (end < (dominoes.size()) && dominoes[end] == L) + right_l = true; + + if (left_r && right_l) { + /* + * 3 4 5 6 7 + * . . . . L + */ + auto difference = end - start; + auto steps = difference / 2; + + for (auto i = 0; i < steps; ++i) { + dominoes[start + i] = R; + dominoes[end - 1 - i] = L; + } + } else if (left_r) { + for (auto i = start; i < end; ++i) dominoes[i] = R; + } else if (right_l) { + for (auto i = start; i < end; ++i) dominoes[i] = L; + } + } + public: + std::string pushDominoes(std::string dominoes) { + auto ranges = findRanges(dominoes); + for (const auto& range : ranges) { + auto& [start, end] = range; + std::cout << "[" << start << ", " << end << "]\n"; + processRange(range, dominoes); + } + return dominoes; + } + }; +} + +#include + +TEST(P838_2, 1) { + std::string input = ".L.R...LR..L.."; + Solution s; + auto result = s.pushDominoes(input); + EXPECT_EQ(result, std::string("LL.RR.LLRRLL..")); +} \ No newline at end of file diff --git a/9/9.cpp b/9/9.cpp new file mode 100644 index 0000000..c5bb256 --- /dev/null +++ b/9/9.cpp @@ -0,0 +1,46 @@ +#include +#include + +namespace { + +class Solution { +public: + bool isPalindrome(int x) { + if (x < 0) + return false; + + std::vector v; + v.reserve(32); + + while (x > 0) { + int z = x % 10; + x = x / 10; + v.emplace_back(z); + } + + for (size_t i = 0; i < v.size(); ++i) { + if (v[i] != v[v.size() - 1 - i]) + return false; + } + + return true; + } +}; + +}; + +#include + +Solution s; + +TEST(P9, 1) { + EXPECT_EQ(s.isPalindrome(121), true); +} + +TEST(P9, 2) { + EXPECT_EQ(s.isPalindrome(-121), false); +} + +TEST(P9, 3) { + EXPECT_EQ(s.isPalindrome(10), false); +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..124d1df --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.30) + +project(leetcode) +set(CMAKE_CXX_STANDARD 20) + +find_package(GTest REQUIRED) +enable_testing() + +file(GLOB sources main.cpp **/*.cpp) + +add_executable(leetcode ${sources}) +target_link_libraries(leetcode GTest::gtest_main) + +include(GoogleTest) +gtest_discover_tests(leetcode) \ No newline at end of file diff --git a/justfile b/justfile new file mode 100644 index 0000000..2f640a5 --- /dev/null +++ b/justfile @@ -0,0 +1,9 @@ +build: + cmake -B ./build -S . + cmake --build ./build --config Debug --target all -j 20 -- + +test: build + ctest -j10 -C Debug -T test --test-dir ./build --output-on-failure + +problem PROBLEM: build + ctest -j10 -C Debug -T test --test-dir ./build --output-on-failure -R ^P{{PROBLEM}} \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..909d2ff --- /dev/null +++ b/main.cpp @@ -0,0 +1,6 @@ +#include + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file