1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int maxPeopleInTower(vector<int>& heights, vector<int>& weights) {
int n = heights.size();
vector<pair<int, int>> people;
// Combine heights and weights into pairs
for (int i = 0; i < n; i++) {
people.push_back({heights[i], weights[i]});
}
// Sort based on height, and break ties by weight
sort(people.begin(), people.end(), [](pair<int, int>& a, pair<int, int>& b) {
if (a.first == b.first)
return a.second < b.second;
return a.first < b.first;
});
// Find LIS based on weights
vector<int> dp(n, 1); // Each person can be a tower on their own
int maxTower = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (people[i].second > people[j].second) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
maxTower = max(maxTower, dp[i]);
}
return maxTower;
}
};
int main() {
vector<int> heights = {65, 70, 56, 75, 60, 68};
vector<int> weights = {100, 150, 90, 200, 95, 110};
Solution solution;
cout << solution.maxPeopleInTower(heights, weights) << endl; // Output: 4
return 0;
}
|