Initial commit

This commit is contained in:
Ilya Volchenkov 2025-05-21 14:26:13 +03:00
commit b33e3df329
11 changed files with 439 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build

19
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -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
}

65
2/2.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <cstdint>
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 <gtest/gtest.h>
TEST(P2, 1) {
Solution s;
auto res = s.addTwoNumbers(nullptr, nullptr);
}

43
2558/2558.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
void print(std::vector<int>& gifts) {
for (auto gift : gifts)
std::cout << gift << ' ';
std::cout << "\n---\n";
}
namespace {
class Solution {
public:
long long pickGifts(std::vector<int>& 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 <gtest/gtest.h>
TEST(P2558, 1) {
std::vector<int> gifts = {1,1,1,1};
int k = 4;
Solution s;
auto res = s.pickGifts(gifts, k);
}

71
3341/3341.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <vector>
#include <queue>
#include <set>
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<std::vector<int>>& 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<Node> open_list;
open_list.push(start_node);
std::set<Node> closed_set;
while (open_list.size())
{
Node current_node = open_list.top();
open_list.pop();
if (current_node == end_node) {
std::vector<Coord> path;
// while (current_node)
}
}
return 0;
}
};

74
838/838-1.cpp Normal file
View File

@ -0,0 +1,74 @@
#include <string>
#include <iostream>
#include <set>
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 <gtest/gtest.h>
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.."));
}

90
838/838-2.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <string>
#include <iostream>
#include <vector>
#include <span>
namespace {
enum Dominoes : char {
Dot = '.',
L = 'L',
R = 'R',
};
using Range = std::tuple<size_t, size_t>;
using Ranges = std::vector<Range>;
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 <gtest/gtest.h>
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.."));
}

46
9/9.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <iostream>
#include <vector>
namespace {
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0)
return false;
std::vector<int> 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 <gtest/gtest.h>
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);
}

15
CMakeLists.txt Normal file
View File

@ -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)

9
justfile Normal file
View File

@ -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}}

6
main.cpp Normal file
View File

@ -0,0 +1,6 @@
#include <gtest/gtest.h>
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}