-
Notifications
You must be signed in to change notification settings - Fork 15
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
H0p1ty
wants to merge
15
commits into
AlgorithmsDafeMipt2024:main
Choose a base branch
from
H0p1ty:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
task_01 #15
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5723e72
test commit
H0p1ty cf43a75
test_commit
H0p1ty cc0495e
task_01 completed
H0p1ty 40c61e7
lesson3
H0p1ty da7e472
test commit
H0p1ty 7ed00bb
task_01 fixes
H0p1ty 7acfbeb
note fix
H0p1ty 44cbf54
pre-fix commit
H0p1ty 85acfc8
lesson5
H0p1ty 51f832e
task_01 fixed x2
H0p1ty 61a3916
Merge branch 'AlgorithmsDafeMipt2024:main' into main
H0p1ty 814b95d
Merge branch 'lessons'
H0p1ty f387f87
Merge branch 'AlgorithmsDafeMipt2024:main' into main
H0p1ty fb20d8a
Merge branch 'AlgorithmsDafeMipt2024:main' into main
H0p1ty f32c7dc
question_mark
H0p1ty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <vector> | ||
//#include <priority_queue> | ||
|
||
// 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) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
if you see this it means that the commit worked | ||
H0p1ty marked this conversation as resolved.
Show resolved
Hide resolved
|
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
#include <iostream> | ||
|
||
int main() { return 0; } | ||
int main() | ||
{ | ||
std::cout << "Hello world!"; | ||
return 0; | ||
} |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,51 @@ | ||
#include <iostream> | ||
#include <unordered_map> | ||
|
||
int main() { return 0; } | ||
/* | ||
|
||
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) | ||
|
||
void solution() { | ||
int number, n, t; | ||
bool flag = false; // in case there are no such numbers | ||
std::unordered_map<int, int> mp; | ||
H0p1ty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
std::cin >> number >> n; | ||
H0p1ty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for (int i = 0; i < n; i++) { | ||
std::cin >> t; | ||
|
||
if (mp.find(number - t) != | ||
mp.end()) { // if key "number - t" exists, we have found the solution | ||
std::cout << mp[number - t] << ' ' << i; | ||
flag = true; | ||
break; | ||
} | ||
|
||
if (mp.find(t) == mp.end()) | ||
mp[t] = 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) std::cout << -1 << ' ' << -1; | ||
} | ||
|
||
int main() { | ||
solution(); | ||
H0p1ty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,61 @@ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <vector> | ||
|
||
#include "topology_sort.hpp" | ||
|
||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
std::pair<int, int> solution(int number, std::vector<int> v) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. вынеси в отдельный файл There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Сделано |
||
bool flag = false; // in case there are no such numbers | ||
std::unordered_map<int, int> mp; | ||
|
||
for (int i = 0; i < v.size(); i++) { | ||
if (mp.find(number - v[i]) != | ||
mp.end()) { // if key "number - t" exists, we have found the solution | ||
return {mp[number - v[i]], i}; | ||
flag = true; | ||
break; | ||
} | ||
|
||
if (mp.find(v[i]) == mp.end()) | ||
mp[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}; | ||
} | ||
|
||
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)); | ||
LostPointer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
это наверное нужно утащить в другой ПР со вторым заданием