Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 71 additions & 33 deletions c++/Chapter 11/Chapter11_7.cpp
Original file line number Diff line number Diff line change
@@ -1,51 +1,89 @@
#include <algorithm>
#include <iostream>
#include <stack>
#include <tuple>
#include <vector>

using namespace std;

struct htWt {
struct Person {
int height, weight;
};

void sortHeight(htWt arr[], int n) {
for (int i=0; i<n; i++) {
for (int j=0; j<n-1; j++){
if (arr[j].height>arr[j+1].height) {
htWt tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
bool operator<(const Person& lhs, const Person& rhs) {
return std::tie(lhs.height, lhs.weight) < std::tie(rhs.height, rhs.weight);
}

// Time complexity: O(n + n * log(n)) = O(n * log(n))
// Space complexity: O(1)
template<typename T>
unsigned int CalculateTower(std::vector<T>& persons) {
make_heap(persons.begin(), persons.end());
auto rit = persons.end();

unsigned int result = 0;
Person current{std::numeric_limits<int>::max(), std::numeric_limits<int>::max()};

while (rit >= persons.begin()) {
if (persons.front() < current) {
current = persons.front();
result++;
}

pop_heap(persons.begin(), rit);
rit = prev(rit);
}

return result;
}

int getIncreasingSequence(htWt arr[], int n) {
sortHeight(arr, n);
vector<vector<int> > wt;
for (int i=0; i<n; i++) {
bool flag = true;
for (int j=0; j<wt.size(); j++) {
if (arr[i].weight >= wt[j].back()) {
flag = false;
wt[j].push_back(arr[i].weight);
// Time complexity: O(n^2)
// Space complexity: O(n)
template<typename T, typename Comparator = std::less<T>>
unsigned int LongestIncreasingSubsequence(const std::vector<T>& values, Comparator comp = Comparator()) {
vector<unsigned int> l(values.size(), 1);
unsigned int best = 0;
for (int i = 1; i < values.size(); i++) {
unsigned int max = 0;
for (int j = i - 1; j >= 0; j--) {
if (comp(values[j], values[i])) {
max = std::max(max, l[j]);
}
}
if (flag) {
vector<int> v;
v.push_back(arr[i].weight);
wt.push_back(v);
}
l[i] = ++max;
best = std::max(best, max);
}
int max = 0;
for (int i=0; i<wt.size(); i++) {
if (max < wt[i].size())
max = wt[i].size();
}
return max;

return best;
}

int main(){
htWt arr[] = {{12, 13}, {11, 15}, {9, 20}, {20, 20}, {40, 21}, {8, 42}};
cout<<getIncreasingSequence(arr, 6);
int main() {
vector<Person> persons{{12, 13}, {11, 15}, {9, 20}, {20, 20}, {40, 21}, {8, 42}};

// We calculate the maximum height of a tower of persons by relying on a max-heap,
// popping persons of the heap onto the tower if the current max element in the heap
// is strictly smaller than the current top-most person in the tower. If it is not strictly
// smaller, we just drop the person as they are "as good as"/"incomparable to" the person
// already standing on top of the tower. Please note that this works because we are not maximizing
// the total height of the tower, but only the number of people being involved in it.
{
vector<Person> cpersons = persons;
cout << "With heap: " << CalculateTower(cpersons) << "\n";
}
// We are not altering the original sequence as in the original problem statement for
// the longest increasing subsequence problem. For the example setup presented here,
// the longest strictly increasing subsequence is 3.
{
cout << "Without sorting: " << LongestIncreasingSubsequence(persons) << "\n";
}
// We are mutating the original sequence of values here, requiring O(n log(n)) to do so.
// Alternatively, we could just rely on a heap to achieve the same functionality.
{
vector<Person> cpersons(persons);
sort(cpersons.begin(), cpersons.end(), [](const Person& lhs, const Person& rhs) {
return lhs.height < rhs.height;
});
cout << "With sorting by height: " << LongestIncreasingSubsequence(cpersons) << "\n";
}
return 0;
}
}