Algorithm
Human Towering
Computational Methods
Structure Stability
Performance Optimization

Algorithm for Human Towering

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

An "algorithm for human towering" is usually a stacking problem: given people with size or strength constraints, find the tallest valid stack. In computer-science terms, this is often modeled as a dynamic-programming problem very close to the classic circus-tower or box-stacking family of problems.

Define the Constraint First

There are at least two common versions of the problem:

  1. each person above must be shorter and lighter than the person below,
  2. or each person below must be strong enough to support the total weight above them.

Those lead to different algorithms. The simpler and more common interview-style version is the first one, where each person has:

  • a height,
  • and a weight.

The goal is to build the longest sequence such that both height and weight increase as you go from top to bottom.

A Dynamic-Programming Model

Suppose each person is represented as (height, weight). One valid tower must satisfy:

text
h1 < h2 < h3 < ...
w1 < w2 < w3 < ...

If you sort the people and then compute the longest compatible chain, you get a dynamic-programming solution.

Here is a simple Python implementation:

python
1def tallest_tower(people):
2    people = sorted(people, key=lambda p: (p[0], p[1]))
3    n = len(people)
4
5    dp = [1] * n
6    prev = [-1] * n
7
8    for i in range(n):
9        for j in range(i):
10            if (
11                people[j][0] < people[i][0]
12                and people[j][1] < people[i][1]
13                and dp[j] + 1 > dp[i]
14            ):
15                dp[i] = dp[j] + 1
16                prev[i] = j
17
18    best_index = max(range(n), key=lambda i: dp[i])
19
20    tower = []
21    while best_index != -1:
22        tower.append(people[best_index])
23        best_index = prev[best_index]
24
25    return tower[::-1]
26
27
28people = [
29    (65, 100),
30    (70, 150),
31    (56, 90),
32    (75, 190),
33    (60, 95),
34    (68, 110),
35]
36
37print(tallest_tower(people))

This returns one tallest valid ordering under the "smaller person on larger person" rule.

Why Sorting Helps

Sorting does not solve the whole problem, but it reduces the search space. After sorting by height and weight, the dynamic program only needs to look backward for valid predecessors.

That makes the solution easier to reason about:

  • sort,
  • compute best tower ending at each person,
  • reconstruct the best sequence.

The time complexity of this basic approach is O(n^2) because each person can compare against earlier people.

A Support-Capacity Variant

Sometimes the problem is not just "lighter and shorter." Instead, each person may have:

  • a weight,
  • and a carrying capacity.

Then validity depends on the total supported weight above them, not just pairwise ordering.

That version is closer to knapsack-style DP. For example, if each person is (weight, strength), one bottom person is valid only if:

text
total_weight_above <= strength

A simplified DP idea is:

  • sort candidates by strength or remaining support margin,
  • track the lightest total stack weight achievable for each tower height,
  • update the DP as you add people.

That is a different optimization problem from simple height-and-weight ordering, even though both can be described informally as "human towering."

Greedy Approaches Usually Fail

It is tempting to sort by one ratio such as:

  • strongest first,
  • lightest first,
  • or best strength-to-weight ratio.

Those rules can look good locally and still produce a suboptimal tower globally. This is why dynamic programming is a better default for the combinatorial version of the problem.

A greedy rule only works if you can prove that local choices preserve global optimality. In human-tower-style problems, that is often not true.

Reconstructing the Tower Matters

Many algorithms compute only the height of the best tower. In practice, you usually want the actual order of people too. That is why the example stores a prev array and reconstructs the solution at the end.

The output order is often the part that matters operationally, not just the numeric optimum.

When the Problem Becomes Harder

The problem becomes more realistic and more complex if you add:

  • multiple people per layer,
  • balance constraints,
  • fatigue or safety margins,
  • or geometric placement rules rather than pure vertical stacking.

At that point, the model can move from simple dynamic programming into graph search, integer programming, or physics-informed optimization.

Common Pitfalls

The biggest pitfall is not defining the constraint precisely. "Tallest stable human tower" can mean pairwise height/weight ordering, support capacity, or full balance physics, and those are different problems.

Another mistake is using a greedy heuristic without proving it is correct. Many natural-looking local rules fail on small counterexamples.

Developers also sometimes compute only the maximum height and forget to reconstruct the actual tower arrangement.

Finally, if equal heights or equal weights are allowed, be explicit about whether equality is valid or whether the inequalities must be strict.

Summary

  • Human towering is usually modeled as a constrained stacking problem.
  • The common height-and-weight version is well suited to dynamic programming.
  • A simple O(n^2) DP can find and reconstruct the tallest valid tower.
  • Support-capacity variants are different and often require a different DP model.
  • The algorithm depends entirely on the exact stability rule you are trying to enforce.

Course illustration
Course illustration

All Rights Reserved.