-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add solutions to 5 and 6 from sorting problems
- Loading branch information
1 parent
133ad82
commit f302184
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,37 @@ | ||
/* | ||
T(n,k) = O(k) | ||
M(n,k) = O(k) | ||
algorytm HUffmana | ||
*/ | ||
|
||
#include <algorithm> | ||
#include <vector> | ||
// #include <deque> | ||
#include <queue> | ||
|
||
float get_min_and_increment(std::vector<float>::const_iterator it, std::queue<float> q) { | ||
float min; | ||
if (*it == std::min(*it, q.front())) { | ||
min = *it; | ||
*it++; | ||
} else { | ||
min = q.front(); | ||
q.pop(); | ||
} | ||
return min; | ||
} | ||
|
||
float beakers(const std::vector<float> v, int k) { | ||
float min_sum; | ||
auto it = v.begin(); | ||
std::queue<float> q; | ||
|
||
for (int i = 0; i < k; i++) { | ||
min_sum = get_min_and_increment(it, q); | ||
min_sum += get_min_and_increment(it, q); | ||
q.push(min_sum); | ||
} | ||
|
||
return std::min(*it, q.front()); | ||
} |
Empty file.