diff --git a/task_01/README.md b/task_01/README.md index 8ceb5d4e..8719ff5f 100644 --- a/task_01/README.md +++ b/task_01/README.md @@ -1,3 +1,3 @@ # Задача 1 -Дано целое число и отсортированый массив целых чисел, нужно найти 2 числа из массива которые в сумме дадут заданное число +Дано целое число и отсортированый массив целых чисел, нужно найти 2 числа из массива которые в сумме дадут заданное число \ No newline at end of file diff --git a/task_01/file.txt b/task_01/file.txt new file mode 100644 index 00000000..d800886d --- /dev/null +++ b/task_01/file.txt @@ -0,0 +1 @@ +123 \ No newline at end of file diff --git a/task_01/src/task.cpp b/task_01/src/task.cpp new file mode 100644 index 00000000..319120f2 --- /dev/null +++ b/task_01/src/task.cpp @@ -0,0 +1,65 @@ +#include "task.hpp" + +// O(logn) +int BinSearch(std::vector massive, int num) { + int length = massive.size(); + int left = 0; + int right = length - 1; + int index = length / 2; + while (right - left >= 0) { + if (massive[index] == num) { + return index; + } else if (massive[index] > num) { + right = index - 1; + } else if (massive[index] < num) { + left = index + 1; + } + index = (right + left) / 2; + } + return -1; +} + +// O(n^2) +std::tuple NSquareSearch(std::vector massive, int num) { + int length = massive.size(); + for (int i = 0; i < length; i++) { + for (int j = i + 1; j < length; j++) { + if (massive[i] + massive[j] == num) { + return {i, j}; + } + } + } + return {-1, -1}; +} + +// O(nlogn) +std::tuple NLogNSearch(std::vector massive, int num) { + int length = massive.size(); + for (int i = 0; i < length; i++) { + int el1 = massive[i]; + int el2 = num - el1; + int j = BinSearch(massive, el2); + if (j != -1 && i != j) { + return {i, j}; + } + } + return {-1, -1}; +} + +// O(n) +std::tuple NSearch(std::vector massive, int num) { + int length = massive.size(); + int left = 0; + int right = length - 1; + while (right != left) { + int sum = massive[left] + massive[right]; + if (sum == num) { + return {left, right}; + } else if (sum < num) { + left++; + } else if (sum > num) { + right--; + } + } + return {-1, -1}; +} \ No newline at end of file diff --git a/task_01/src/task.hpp b/task_01/src/task.hpp new file mode 100644 index 00000000..437493fd --- /dev/null +++ b/task_01/src/task.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include +#include +#include + +// O(logn) +int BinSearch(std::vector mas, int num); + +// O(n^2) +std::tuple NSquareSearch(std::vector massive, int num); + +// O(nlogn) +std::tuple NLogNSearch(std::vector massive, int num); + +// O(n) +std::tuple NSearch(std::vector massive, int num); \ No newline at end of file diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index ef5a86ae..96496788 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,8 +1,83 @@ #include -#include "topology_sort.hpp" +#include "task.hpp" -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +// BinSearch tests +TEST(BinSearch, Simple) { + // even len + std::vector mas = {1, 2, 3, 4, 5, 6, 7, 8}; + ASSERT_EQ(BinSearch(mas, 1), 0); + ASSERT_EQ(BinSearch(mas, 2), 1); + ASSERT_EQ(BinSearch(mas, 3), 2); + ASSERT_EQ(BinSearch(mas, 4), 3); + ASSERT_EQ(BinSearch(mas, 5), 4); + ASSERT_EQ(BinSearch(mas, 6), 5); + ASSERT_EQ(BinSearch(mas, 7), 6); + ASSERT_EQ(BinSearch(mas, 8), 7); + + ASSERT_EQ(BinSearch(mas, 31), -1); + + // odd len + mas = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + ASSERT_EQ(BinSearch(mas, 1), 0); + ASSERT_EQ(BinSearch(mas, 2), 1); + ASSERT_EQ(BinSearch(mas, 3), 2); + ASSERT_EQ(BinSearch(mas, 4), 3); + ASSERT_EQ(BinSearch(mas, 5), 4); + ASSERT_EQ(BinSearch(mas, 6), 5); + ASSERT_EQ(BinSearch(mas, 7), 6); + ASSERT_EQ(BinSearch(mas, 8), 7); + + ASSERT_EQ(BinSearch(mas, 31), -1); + + // empty mas + mas = {}; + ASSERT_EQ(BinSearch(mas, 8), -1); + + // mas of 1 elem + mas = {1}; + ASSERT_EQ(BinSearch(mas, 1), 0); + + ASSERT_EQ(BinSearch(mas, 31), -1); + + // mas of 2 elems + mas = {1, 2}; + ASSERT_EQ(BinSearch(mas, 1), 0); + ASSERT_EQ(BinSearch(mas, 2), 1); + + ASSERT_EQ(BinSearch(mas, 31), -1); } + +// NSquareSearch tests +TEST(NSquareSearch, Simple) { + std::vector mas = {1, 2, 3, 4, 5, 6, 7, 8}; + ASSERT_EQ(NSquareSearch(mas, 11), std::make_tuple(2, 7)); + ASSERT_EQ(NSquareSearch(mas, 9), std::make_tuple(0, 7)); + + ASSERT_EQ(NSquareSearch(mas, 1), std::make_tuple(-1, -1)); + ASSERT_EQ(NSquareSearch(mas, 16), + std::make_tuple(-1, -1)); // not (7, 7), as indexes cant be same +} + +// NLogNSearch tests +TEST(NLogNSearch, Simple) { + std::vector mas = {1, 2, 3, 4, 5, 6, 7, 8}; + ASSERT_EQ(NLogNSearch(mas, 11), std::make_tuple(2, 7)); + ASSERT_EQ(NLogNSearch(mas, 9), std::make_tuple(0, 7)); + + ASSERT_EQ(NLogNSearch(mas, 1), std::make_tuple(-1, -1)); + ASSERT_EQ(NLogNSearch(mas, 16), + std::make_tuple(-1, -1)); // not (7, 7), as indexes cant be same +} + +// NSearch tests +TEST(NSearch, Simple) { + std::vector mas = {1, 2, 3, 4, 5, 6, 7, 8}; + ASSERT_EQ(NSearch(mas, 11), std::make_tuple(2, 7)); + ASSERT_EQ(NSearch(mas, 9), std::make_tuple(0, 7)); + + ASSERT_EQ(NSearch(mas, 1), std::make_tuple(-1, -1)); + ASSERT_EQ(NSearch(mas, 16), + std::make_tuple(-1, -1)); // not (7, 7), as indexes cant be same +} \ No newline at end of file diff --git a/task_01/src/topology_sort.cpp b/task_01/src/topology_sort.cpp deleted file mode 100644 index e53f670c..00000000 --- a/task_01/src/topology_sort.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "topology_sort.hpp" diff --git a/task_01/src/topology_sort.hpp b/task_01/src/topology_sort.hpp deleted file mode 100644 index 6f70f09b..00000000 --- a/task_01/src/topology_sort.hpp +++ /dev/null @@ -1 +0,0 @@ -#pragma once diff --git a/task_02/src/stack.cpp b/task_02/src/stack.cpp index 8ca89902..401b5723 100644 --- a/task_02/src/stack.cpp +++ b/task_02/src/stack.cpp @@ -1,21 +1,35 @@ #include "stack.hpp" #include - -void Stack::Push(int value) { data_.push(value); } +#include +#include int Stack::Pop() { - auto result = data_.top(); - data_.pop(); + int result = data_[data_.size() - 1]; + data_.pop_back(); return result; } -void MinStack::Push(int value) { data_.push_back(value); } +void MinStack::Push(int value) { + std::pair new_element; + if (current_minimum_ == 1e9) { + new_element = {value, value}; + } else if (value < current_minimum_) { + new_element = {value, value}; + } else { + new_element = {value, current_minimum_}; + } + data_.push_back(new_element); + current_minimum_ = new_element.second; +} int MinStack::Pop() { - auto result = data_.back(); + int result = data_[data_.size() - 1].first; data_.pop_back(); + if (data_.size() == 0) { + current_minimum_ = 1e9; + } else { + current_minimum_ = data_[data_.size() - 1].second; + } return result; } - -int MinStack::GetMin() { return *std::min_element(data_.begin(), data_.end()); } \ No newline at end of file diff --git a/task_02/src/stack.hpp b/task_02/src/stack.hpp index 138ec40f..3a485672 100644 --- a/task_02/src/stack.hpp +++ b/task_02/src/stack.hpp @@ -1,23 +1,25 @@ #pragma once #include +#include #include class Stack { public: - void Push(int value); + void Push(int value) { data_.push_back(value); } int Pop(); private: - std::stack data_; + std::vector data_; }; class MinStack { public: void Push(int value); int Pop(); - int GetMin(); + int GetMin() { return current_minimum_; }; private: - std::vector data_; + std::vector> data_; + int current_minimum_ = 1e9; };