-
Notifications
You must be signed in to change notification settings - Fork 15
homework_1 #7
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
sh1buya
wants to merge
14
commits into
AlgorithmsDafeMipt2024:main
Choose a base branch
from
sh1buya:homework1
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
homework_1 #7
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
98fe647
first_commit
sh1buya 53d9818
tst
sh1buya 10c8c43
tst
sh1buya 773c6fd
tst
sh1buya 0512b72
task without tests done
sh1buya aafa1be
tests done
sh1buya 1322db1
Merge branch 'main' into homework1
sh1buya 639824c
names fixed
sh1buya b9604f4
Merge branch 'homework1' of https://github.com/sh1buya/spring_homewor…
sh1buya d44edbe
task_02 commited
sh1buya 1376e84
stack fixed
sh1buya 4e633c6
files renamed and tests in stack are done correctly
sh1buya 2e701d6
one line corrected
sh1buya 55b421b
one line corrected
sh1buya 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Задача 1 | ||
|
||
Дано целое число и отсортированый массив целых чисел, нужно найти 2 числа из массива которые в сумме дадут заданное число | ||
Дано целое число и отсортированый массив целых чисел, нужно найти 2 числа из массива которые в сумме дадут заданное число |
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 @@ | ||
123 |
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
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 +1,65 @@ | ||
#include "topology_sort.hpp" | ||
|
||
// O(logn) | ||
int BinSearch(std::vector<int> 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<int, int> NSquareSearch(std::vector<int> 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<int, int> NLogNSearch(std::vector<int> 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<int, int> NSearch(std::vector<int> 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}; | ||
} |
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. давай файлы переименуем с тем что в них содержится |
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 +1,17 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
#include <tuple> | ||
#include <vector> | ||
|
||
// O(logn) | ||
int BinSearch(std::vector<int> mas, int num); | ||
|
||
// O(n^2) | ||
std::tuple<int, int> NSquareSearch(std::vector<int> massive, int num); | ||
|
||
// O(nlogn) | ||
std::tuple<int, int> NLogNSearch(std::vector<int> massive, int num); | ||
|
||
// O(n) | ||
std::tuple<int, int> NSearch(std::vector<int> massive, int num); |
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. это же 1ое задание? тогда верни как было, что бы тесты проходили |
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,21 +1,35 @@ | ||
#include "stack.hpp" | ||
|
||
#include <algorithm> | ||
|
||
void Stack::Push(int value) { data_.push(value); } | ||
#include <cstddef> | ||
#include <utility> | ||
|
||
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<int, int> new_element; | ||
if (current_minimum_ == NULL) { | ||
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_ = NULL; | ||
} else { | ||
current_minimum_ = data_[data_.size() - 1].second; | ||
} | ||
return result; | ||
} | ||
|
||
int MinStack::GetMin() { return *std::min_element(data_.begin(), data_.end()); } |
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,23 +1,25 @@ | ||
#pragma once | ||
|
||
#include <stack> | ||
#include <utility> | ||
#include <vector> | ||
|
||
class Stack { | ||
public: | ||
void Push(int value); | ||
void Push(int value) { data_.push_back(value); } | ||
int Pop(); | ||
|
||
private: | ||
std::stack<int> data_; | ||
std::vector<int> data_; | ||
}; | ||
|
||
class MinStack { | ||
public: | ||
void Push(int value); | ||
int Pop(); | ||
int GetMin(); | ||
int GetMin() { return current_minimum_; }; | ||
|
||
private: | ||
std::vector<int> data_; | ||
std::vector<std::pair<int, int>> data_; | ||
int current_minimum_; | ||
}; |
Oops, something went wrong.
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.
как отличить результат (-1, -1) от отсутствия результата?
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.
такого результата не может быть ни в каком случае, оба числа это индексы от 0 до len-1
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.
ок