Skip to content

Commit 8bea497

Browse files
committed
task 2-4
1 parent 74ad1a1 commit 8bea497

File tree

15 files changed

+142
-29
lines changed

15 files changed

+142
-29
lines changed

task_01/src/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
#include <gtest/gtest.h>
33

4-
#include "topology_sort.hpp"
4+
#include "utils.hpp"
55

66
TEST(TopologySort, Simple) {
77
ASSERT_EQ(1, 1); // Stack []

task_01/src/topology_sort.cpp

Lines changed: 0 additions & 1 deletion
This file was deleted.

task_01/src/utils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "utils.hpp"
File renamed without changes.

task_02/src/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#include <iostream>
22

3+
#include "stack.hpp"
34
int main() { return 0; }

task_02/src/stack.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,36 @@
22

33
#include <algorithm>
44

5-
void Stack::Push(int value) { data_.push(value); }
5+
void Stack::Push(int value) {
6+
Node* t = new Node();
7+
t->value_ = value;
8+
if (this->top_ == nullptr) {
9+
this->top_ = t;
10+
} else {
11+
t->prev_ = this->top_;
12+
this->top_ = t;
13+
}
14+
}
615

716
int Stack::Pop() {
8-
auto result = data_.top();
9-
data_.pop();
10-
return result;
17+
Node* t = this->top_;
18+
this->top_ = this->top_->prev_;
19+
return t->value_;
1120
}
1221

13-
void MinStack::Push(int value) { data_.push_back(value); }
22+
Node* Stack::Top() { return this->top_; }
23+
void MinStack::Push(int value) {
24+
if (s_.Top() == nullptr) {
25+
m_.Push(value);
26+
} else {
27+
m_.Push(std::min(value, s_.Top()->value_));
28+
}
29+
s_.Push(value);
30+
}
1431

1532
int MinStack::Pop() {
16-
auto result = data_.back();
17-
data_.pop_back();
18-
return result;
33+
int t = m_.Pop();
34+
return s_.Pop();
1935
}
2036

21-
int MinStack::GetMin() { return *std::min_element(data_.begin(), data_.end()); }
37+
int MinStack::GetMin() { return m_.Top()->value_; }

task_02/src/stack.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
#pragma once
22

3-
#include <stack>
4-
#include <vector>
3+
struct Node {
4+
int value_ = 0;
5+
Node* prev_ = nullptr;
6+
};
57

6-
class Stack {
7-
public:
8+
struct Stack {
89
void Push(int value);
910
int Pop();
11+
Node* Top();
1012

11-
private:
12-
std::stack<int> data_;
13+
Node* top_ = nullptr;
1314
};
1415

15-
class MinStack {
16-
public:
16+
struct MinStack {
1717
void Push(int value);
1818
int Pop();
1919
int GetMin();
2020

21-
private:
22-
std::vector<int> data_;
21+
Stack s_;
22+
Stack m_;
2323
};

task_03/src/temp_up_days.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "temp_up_days.hpp"
2+
3+
#include <stack>
4+
5+
std::vector<int> TempUpDayCounter(std::vector<int> temps) {
6+
std::stack<Day> days;
7+
std::vector<int> result(temps.size(), 0);
8+
for (int i = 0; i < temps.size(); i++) {
9+
Day d;
10+
d.index_ = i;
11+
d.temp_ = temps[i];
12+
while (!days.empty()) {
13+
if (days.top().temp_ < d.temp_) {
14+
result[days.top().index_] = d.index_ - days.top().index_;
15+
days.pop();
16+
} else {
17+
break;
18+
}
19+
}
20+
days.push(d);
21+
}
22+
return result;
23+
}

task_03/src/temp_up_days.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
#include <vector>
3+
4+
struct Day {
5+
int index_;
6+
int temp_;
7+
};
8+
9+
std::vector<int> TempUpDayCounter(std::vector<int> temps);

task_03/src/test.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11

22
#include <gtest/gtest.h>
33

4-
#include "topology_sort.hpp"
4+
#include "temp_up_days.hpp"
55

6-
TEST(TopologySort, Simple) {
7-
ASSERT_EQ(1, 1); // Stack []
6+
TEST(TempUpDays, Simple) {
7+
ASSERT_EQ(TempUpDayCounter(std::vector<int>{5, 7, 4, 5}),
8+
(std::vector<int>{1, 0, 1, 0}));
9+
ASSERT_EQ(TempUpDayCounter(std::vector<int>{5, 12, 4, 9, 5, 4, 2}),
10+
(std::vector<int>{1, 0, 1, 0, 0, 0, 0}));
11+
ASSERT_EQ(TempUpDayCounter(std::vector<int>{2, 6, 17, 7, 3, 4}),
12+
(std::vector<int>{1, 1, 0, 0, 1, 0}));
813
}

task_03/src/topology_sort.cpp

Lines changed: 0 additions & 1 deletion
This file was deleted.

task_03/src/topology_sort.hpp

Lines changed: 0 additions & 1 deletion
This file was deleted.

task_04/src/heap.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "heap.hpp"
2+
3+
#include <vector>
4+
5+
void Heap::SiftUp(int index) {
6+
while (heap_[index] < heap_[(index - 1) / 2]) {
7+
std::swap(heap_[index], heap_[(index - 1) / 2]);
8+
index = (index - 1) / 2;
9+
}
10+
}
11+
12+
void Heap::SiftDown(int index) {
13+
while (2 * index + 1 < heap_.size()) {
14+
int l = 2 * index + 1, r = 2 * index + 2;
15+
int j = l;
16+
if (r < heap_.size() && heap_[r] < heap_[l]) {
17+
j = r;
18+
}
19+
if (heap_[index] < heap_[j]) {
20+
break;
21+
}
22+
std::swap(heap_[index], heap_[j]);
23+
index = j;
24+
}
25+
}
26+
27+
int Heap::Min() { return heap_[0]; }
28+
29+
void Heap::Insert(int value) {
30+
heap_.push_back(value);
31+
this->SiftUp(heap_.size() - 1);
32+
}
33+
34+
void Heap::Build(std::vector<int> data) {
35+
for (auto x : data) {
36+
this->Insert(x);
37+
}
38+
}
39+
40+
int FindMin(std::vector<int> data) {
41+
Heap heap;
42+
heap.Build(data);
43+
return heap.Min();
44+
}

task_04/src/heap.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include <vector>
4+
struct Heap {
5+
void SiftUp(int index);
6+
void SiftDown(int index);
7+
int Min();
8+
void Insert(int value);
9+
void Build(std::vector<int> data);
10+
11+
std::vector<int> heap_;
12+
};
13+
14+
int FindMin(std::vector<int> data);

task_04/src/test.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
21
#include <gtest/gtest.h>
32

4-
TEST(TopologySort, Simple) {
5-
ASSERT_EQ(1, 1); // Stack []
3+
#include "heap.hpp"
4+
5+
TEST(HeapMin, Simple) {
6+
ASSERT_EQ(FindMin(std::vector<int>{4, 5, 16, 3, 6}), 3);
7+
ASSERT_EQ(FindMin(std::vector<int>{29, 25, 10, 13, 14, 23, 4, 6}), 4);
8+
ASSERT_EQ(FindMin(std::vector<int>{29, 17, 16, 27, 6, 11}), 6);
69
}

0 commit comments

Comments
 (0)