Skip to content

task_01 #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
19 changes: 19 additions & 0 deletions lesson3/heap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <vector>
//#include <priority_queue>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

это наверное нужно утащить в другой ПР со вторым заданием


// left child: 2*i + 1;
// right child: 2*i + 2;
// parent: [(i-1)/2];

struct Heap {
Heap() : size{0}, values{} {}

void sift_up(int i);
void sift_down(int i);
void append(int x);

std::vector<int> values;
int size;
};

void Heap::sift_up(int i) {}
21 changes: 21 additions & 0 deletions lesson3/sorting_algorithms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <vector>

// bubble_sort: O*(n^2), O(n^2)
// bogo_sort: O*(n!), O(+inf)
// insertion_sort: O*(n^2), O(n^2)
// selection_sort: O*(n^2), O(n^2)
// merge_sort (top_down/bottom_up): O*(nlogn), O(nlogn)
// quick_sort: O*(nlogn), (O(n^2) - ineffective, O(nlogn) - effective)

// qsort [] = []
// qsort(x : xs) = qsort(filter(<x) xs) ++ [x] (filter(=x) xs) ++
// qsort(filter(>x) xs)

void bubble_sort(std::vector<int>& v) {}

void td_merge_sort(std::vector<int>& v) {
for (int l = 1; l < v.size(); l++) {
for (int i = 0; i + l < v.size(); i += 2 * l) {
}
}
}
1 change: 1 addition & 0 deletions sandbox/template/src/commit_message.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if you see this it means that the commit worked
Binary file added sandbox/template/src/main
Binary file not shown.
5 changes: 4 additions & 1 deletion sandbox/template/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <iostream>

int main() { return 0; }
int main() {
std::cout << "Hello world!";
return 0;
}
Binary file added task_01/src/main
Binary file not shown.
21 changes: 20 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
#include <iostream>

int main() { return 0; }
#include "solution.h"

int main() {
int sum, arr_size, t;
std::vector<int> input_vector;
std::unordered_map<int, int> indices_map; // keeps array numbers as keys and
// indices as values behind keys

std::cin >> sum >> arr_size;

for (int i = 0; i < arr_size; i++) {
std::cin >> t;
input_vector.push_back(t);
}

std::pair<int, int> sol = solution(sum, input_vector);
std::cout << sol.first << ' ' << sol.second;

return 0;
}
44 changes: 44 additions & 0 deletions task_01/src/solution.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <unordered_map>
#include <vector>

/*

Output - indices of two numbers, which sum is equal to needed number, if there's
no such numbers, the output is "-1 -1"

Input:

10
10
-2 2 3 3 5 8 11 13 14 15

Output:

1 5

*/

// Solution below has a time complexity of O(n) and memory complexity of O(n)

std::pair<int, int> solution(int sum, std::vector<int> v) {
bool flag = false; // in case there are no such numbers
std::unordered_map<int, int> indices_map; // keeps array numbers as keys and
// indices as values behind keys

for (int i = 0; i < v.size(); i++) {
if (indices_map.find(sum - v[i]) !=
indices_map
.end()) { // if key "number - t" exists, we have found the solution
return {indices_map[sum - v[i]], i};
flag = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а давай подумаем, мы сюда когда-нибудь попадем?)

break;
}

if (indices_map.find(v[i]) == indices_map.end())
indices_map[v[i]] =
i; // We only add keys that weren't in the map before (that
// way we get the least possible sum of i and j)
}

if (!flag) return {-1, -1};
}
38 changes: 35 additions & 3 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,40 @@

#include <gtest/gtest.h>

#include "solution.h"
#include "topology_sort.hpp"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
}
TEST(solution, simple) {
std::vector<int> v1 = {-2, 2, 3, 3, 5, 9, 11, 13, 14, 15};
std::pair<int, int> p1 = {-1, -1};
ASSERT_EQ(p1, solution(10, v1));

std::vector<int> v2 = {-2, 2, 3, 3, 5, 8, 11, 13, 14, 15};
std::pair<int, int> p2 = {1, 5};
ASSERT_EQ(p2, solution(10, v2));

std::vector<int> v3 = {};
std::pair<int, int> p3 = {-1, -1};
ASSERT_EQ(p3, solution(0, v3));

std::vector<int> v4 = {1};
std::pair<int, int> p4 = {-1, -1};
ASSERT_EQ(p4, solution(1, v4));

std::vector<int> v5 = {1, 2};
std::pair<int, int> p5 = {0, 1};
ASSERT_EQ(p5, solution(3, v5));

// if there are multiple solutions, the algorithm
// will pick the one, in which sum of i and j is the least,
// where i and j are indices of numbers, which sum is equal to needed number
std::vector<int> v6 = {1, 1, 1, 1, 1, 1, 1, 1, 1};
std::pair<int, int> p6 = {0, 1};
ASSERT_EQ(p6, solution(2, v6));

// in case there are multiple solutions in which i+j is the least,
// the algorithm will pick the one, in which i is greater
std::vector<int> v7 = {1, 2, 1, 1, 4, 5, 1, 1, 1};
std::pair<int, int> p7 = {1, 4};
ASSERT_EQ(p7, solution(6, v7));
}
Empty file added testfile.txt
Empty file.