-
Notifications
You must be signed in to change notification settings - Fork 15
Task 1 #20
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
base: main
Are you sure you want to change the base?
Task 1 #20
Changes from 12 commits
d2cdd6b
d3ee901
1dac140
7b6bf9e
b131d11
620272f
8dd910a
7bf3712
8dee7cf
8d5137e
50af8f0
b0026d6
27b4939
172464b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
int main() { return 0; } | ||
#include "utils.hpp" | ||
|
||
int main() { | ||
int size; | ||
std::cin >> size; | ||
std::vector<int> v(size); | ||
for (int i = 0; i < v.size(); i++) { | ||
std::cin >> v[i]; | ||
} | ||
int targetSum; | ||
std::cin >> targetSum; | ||
std::pair<int, int> ans = FindTargetSumInArray(v, targetSum); | ||
if (ans.first != -1) | ||
std::cout << ans.first << " " << ans.second; | ||
else | ||
std::cout << "Not found"; | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,33 @@ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "topology_sort.hpp" | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "utils.hpp" | ||
|
||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
} | ||
std::vector<int> v1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | ||
ASSERT_EQ(FindTargetSumInArray(v1, 0), std::make_pair(-1, -1)); | ||
ASSERT_EQ(FindTargetSumInArray(v1, 1), std::make_pair(0, 1)); | ||
ASSERT_EQ(FindTargetSumInArray(v1, 9), std::make_pair(0, 9)); | ||
ASSERT_EQ(FindTargetSumInArray(v1, 11), std::make_pair(1, 10)); | ||
ASSERT_EQ(FindTargetSumInArray(v1, -5), std::make_pair(-1, -1)); | ||
|
||
std::vector<int> v2{0}; | ||
ASSERT_EQ(FindTargetSumInArray(v2, 0), std::make_pair(-1, -1)); | ||
ASSERT_EQ(FindTargetSumInArray(v2, 47), std::make_pair(-1, -1)); | ||
ASSERT_EQ(FindTargetSumInArray(v2, -47), std::make_pair(-1, -1)); | ||
|
||
std::vector<int> v3{-11, -6, 0, 1, 2, 4, 10, 16}; | ||
ASSERT_EQ(FindTargetSumInArray(v3, -6), std::make_pair(1, 2)); | ||
ASSERT_EQ(FindTargetSumInArray(v3, -7), std::make_pair(0, 5)); | ||
ASSERT_EQ(FindTargetSumInArray(v3, -8), std::make_pair(-1, -1)); | ||
ASSERT_EQ(FindTargetSumInArray(v3, 5), std::make_pair(0, 7)); | ||
ASSERT_EQ(FindTargetSumInArray(v3, 4), std::make_pair(1, 6)); | ||
ASSERT_EQ(FindTargetSumInArray(v3, 10), std::make_pair(1, 7)); | ||
ASSERT_EQ(FindTargetSumInArray(v3, -60), std::make_pair(-1, -1)); | ||
ASSERT_EQ(FindTargetSumInArray(v3, -12), std::make_pair(-1, -1)); | ||
ASSERT_EQ(FindTargetSumInArray(v3, 47), std::make_pair(-1, -1)); | ||
ASSERT_EQ(FindTargetSumInArray(v3, 11), std::make_pair(3, 6)); | ||
} |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "utils.hpp" | ||
|
||
std::pair<int, int> FindTargetSumInArray(std::vector<int> v, int targetSum) { | ||
int i = 0, j = v.size() - 1; | ||
while (v[i] < v[j]) { | ||
if (v[i] + v[j] > targetSum) | ||
j--; | ||
else if (v[i] + v[j] < targetSum) | ||
i++; | ||
else | ||
return std::pair<int, int>(i, j); | ||
} | ||
return std::pair<int, int>(-1, -1); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#pragma once | ||
#include <iostream> | ||
#include <vector> | ||
// Returns pair of indices, which gives in sum target sum | ||
// If target sun is not presented returns pair -1; -1 | ||
// If we target sum presented in several ways returns i, j | ||
// So that i is minimumal index and j is maximum index | ||
std::pair<int, int> FindTargetSumInArray(std::vector<int> v, int targetSum); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
#include <iostream> | ||
|
||
#include "stack.hpp" | ||
|
||
using namespace std; | ||
|
||
int main() { return 0; } |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,103 @@ | ||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <memory> | ||
#include <stack> | ||
#include <stdexcept> | ||
#include <type_traits> | ||
#include <vector> | ||
|
||
template <class T> | ||
class StackNode { | ||
public: | ||
explicit StackNode(T _data, StackNode<T> *_prev = nullptr) | ||
: data(_data), prevptr(_prev) {} | ||
StackNode() : data(), prevptr(nullptr) {} | ||
T data; | ||
StackNode<T> *prevptr; | ||
}; | ||
|
||
template <class T> | ||
class Stack { | ||
public: | ||
void Push(int value); | ||
int Pop(); | ||
Stack() : top(nullptr), size(0) {} | ||
void push(T value); | ||
T pop(); | ||
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. Pop |
||
T getTop(); | ||
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. GetTop |
||
int getSize(); | ||
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. GetSize |
||
|
||
private: | ||
std::stack<int> data_; | ||
StackNode<T> *top; | ||
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. top_ |
||
int size; | ||
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. size_ |
||
}; | ||
|
||
template <class T> | ||
void Stack<T>::push(T value) { | ||
StackNode<T> *newElem = new StackNode<T>(value, top); | ||
size++; | ||
top = newElem; | ||
} | ||
|
||
template <class T> | ||
T Stack<T>::pop() { | ||
if (size == 0) { | ||
throw std::underflow_error("Empty stack"); | ||
} | ||
T returningValue = top->data; | ||
StackNode<T> *oldTop = top; | ||
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. old_top |
||
top = top->prevptr; | ||
delete oldTop; | ||
size--; | ||
return returningValue; | ||
} | ||
|
||
template <class T> | ||
int Stack<T>::getSize() { | ||
return size; | ||
} | ||
|
||
template <class T> | ||
T Stack<T>::getTop() { | ||
if (size == 0) { | ||
throw std::underflow_error("Empty stack"); | ||
} | ||
return top->data; | ||
} | ||
template <class T> | ||
class MinStack { | ||
public: | ||
void Push(int value); | ||
int Pop(); | ||
int GetMin(); | ||
MinStack() : mainStack(), minStack() {} | ||
void push(T value); | ||
T pop(); | ||
T getMin(); | ||
int getSize(); | ||
|
||
private: | ||
std::vector<int> data_; | ||
Stack<T> mainStack; | ||
Stack<T> minStack; | ||
}; | ||
|
||
template <class T> | ||
void MinStack<T>::push(T value) { | ||
mainStack.push(value); | ||
if (minStack.getSize() == 0) | ||
minStack.push(value); | ||
else | ||
minStack.push(std::min(value, minStack.getTop())); | ||
} | ||
|
||
template <class T> | ||
T MinStack<T>::pop() { | ||
minStack.pop(); | ||
return mainStack.pop(); | ||
} | ||
|
||
template <class T> | ||
T MinStack<T>::getMin() { | ||
return minStack.getTop(); | ||
} | ||
|
||
template <class T> | ||
int MinStack<T>::getSize() { | ||
return mainStack.getSize(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# Задача на количество дней перед потеплением | ||
|
||
Написать функцию в которую передается темепература за каждый день в определенный момент день, нужно вернуть количество дней до повышения температуры для каждого дня, если температура не повысится, то для этого дня поставить в результирующий массив 0. | ||
Написать функцию в которую передается темепература за каждый день в определенный момент дня. Вернуть количество дней до повышения температуры для каждого дня. Если температура не повысится, для этого дня поставить в результирующий массив 0. | ||
|
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. weather_report.cpp 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. и в других задачах тоже |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "WeatherReport.h" | ||
|
||
#include <stack> | ||
#include <vector> | ||
|
||
std::vector<int> findNextGreater(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. FindNextGreater |
||
std::vector<int> res(v.size(), -1); | ||
std::stack<int> stack; | ||
|
||
for (int i = v.size() - 1; i >= 0; i--) { | ||
while (stack.size() != 0 && v[i] >= v[stack.top()]) { | ||
stack.pop(); | ||
} | ||
if (stack.size() != 0) { | ||
res[i] = stack.top() - i; | ||
} else { | ||
res[i] = -1; | ||
} | ||
stack.push(i); | ||
} | ||
return res; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#include <stack> | ||
#include <vector> | ||
|
||
std::vector<int> findNextGreater(std::vector<int> v); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#include <iostream> | ||
|
||
#include "WeatherReport.h" | ||
int main() { return 0; } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "topology_sort.hpp" | ||
#include "WeatherReport.h" | ||
|
||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
} | ||
TEST(findNextGreater, Simple) { | ||
ASSERT_EQ(findNextGreater(std::vector<int>{1}), (std::vector<int>{-1})); | ||
ASSERT_EQ(findNextGreater(std::vector<int>{}), (std::vector<int>{})); | ||
ASSERT_EQ(findNextGreater(std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), | ||
(std::vector<int>{1, 1, 1, 1, 1, 1, 1, 1, 1, -1})); | ||
ASSERT_EQ( | ||
findNextGreater(std::vector<int>{16, 19, 10, 14, 22, 27, 12, 10, 16, 24}), | ||
(std::vector<int>{{1, 3, 1, 1, 1, -1, 2, 1, 1, -1}})); | ||
ASSERT_EQ(findNextGreater( | ||
std::vector<int>{-29, 0, -7, -14, 0, -11, -13, -3, -27, -25}), | ||
(std::vector<int>{{1, -1, 2, 1, -1, 2, 1, -1, 1, -1}})); | ||
} |
This file was deleted.
This file was deleted.
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.
prev_ptr