Skip to content
Go back

Maximum Coincident Height: Cylinders and Sets in Python

Published:  at  09:01 PM

Can you imagine stacking cylinders of different heights looking for the perfect point where they all match? 🤔 It’s an optimization challenge with a Tetris flavor!

🔮 Problem Statement

The problem we address is finding the maximum common height among multiple stacks of cylinders, where each cylinder has the same diameter but variable heights within each stack. Formally:

🧩 Step-by-Step Solution

To solve this problem, we adopt a strategy based on sets and cumulative sums. Each stack is transformed into a set of possible achievable heights, and then we look for the intersection of these sets.

First, we need a function that calculates the possible achievable heights in a given stack:

def get_sum_stack(stack):
	acc = 0
	sum_stack = set()
	for n in stack:
		acc += n
		sum_stack.add(acc)
	return sum_stack

This code block iterates over each cylinder in the stack, accumulating its heights in the acc variable. For each accumulated height, we add it to the sum_stack set. The set ensures that there are no duplicate heights, which simplifies subsequent operations. The logic of using a set is that it stores the possible heights of each of the stacks, making it more efficient to find the intersection between the heights of the stacks.

Then, we implement the main function that finds the maximum coinciding height:

def equal_height(stacks):
	"level: medium; points: 5"
	result = get_sum_stack(stacks[0])
	for stack in stacks:
		result &= get_sum_stack(stack)
	return max(result) if len(result) > 0 else 0

This block begins by initializing result with the set of possible heights of the first stack. Then, it iterates over the remaining stacks, performing an intersection with result. The intersection (operator &=) keeps only the heights that are common to all stacks. Finally, if the resulting set is not empty, it returns the maximum height; otherwise, it returns 0.

Complete Solution:

def get_sum_stack(stack):
	acc = 0
	sum_stack = set()
	for n in stack:
		acc += n
		sum_stack.add(acc)
	return sum_stack

def equal_height(stacks):
	"level: medium; points: 5"
	result = get_sum_stack(stacks[0])
	for stack in stacks:
		result &= get_sum_stack(stack)
	return max(result) if len(result) > 0 else 0

🧠 Key Concepts

The solution relies on several key concepts:

💫 Final Thoughts

A possible improvement would be to optimize the get_sum_stack function to avoid recalculating the cumulative sums in each iteration of equal_height, although the cost in terms of asymptotic complexity is minimal and the current code is more readable.

Did you know…? 🤔 The efficiency of set intersection in Python (implemented in C under the hood) makes this solution surprisingly fast, even for large numbers of stacks and cylinders.

I hope this journey through coinciding heights has been enlightening! If you are interested in exploring more algorithms and data structures, feel free to keep reading! I’ll be waiting for you in the next article to continue expanding our programming horizons together! 🚀



Previous Post
Calculate Your Fintech Credit: Commission, VAT, and Amount to Request
Next Post
Decimal to Binary: Convert Numbers with Python (Step by Step)