Spaceballs

I like to hone my problem-solving skills by going over some programming contest questions and I also like to use programming contest problems in interviews because it allows me to gauge whether the candidate is a just a coder or a problem solver. That's not to say that if you can't come up with a correct solution, you aren't a problem solver -- if I'm the interviewer, I'm more interested in how you approach the problem and whether you can break up and present the problem in an abstract manner.

Juliet reads an interesting sci-fi book. In one chapter, the main character needs to solve a problem of maximizing the utilization of cargo spaceships. The ships transport valuable items that have the form of D-dimensional mesh with the size of 3 nodes in each dimension. The nodes are formed by balls of the same weight. The connections between balls are so light that their weight is negligible compared to the weight of balls. This means that the weight of any item is determined solely by the number of its nodes. On the other hand, the value of such an item is equal to the number of nodes plus the number of connections.

Each spaceships has a limited tonnage and we want to maximize the total value of items that maybe stored in the ship without exceeding the tonnage. Your task is to decide what items of what dimension should be loaded to maximize their total value, providing that we have an unlimited supply of items of all dimensions

Input Specification:

The first line of the input contains the number of test cases N. Each test case then consists of a single line containing one positive integer number K < 10,000,000 giving the ship cargo capacity.

Output Specification:

For each test case, print one line containing space-separated non-negative numbers Xm Xm−1 . . . X1 X0, where Xm > 0 and Xi (0 ≤ i ≤ m ) is the number of items of the i-th dimension that we need to store to maximize their total value.

Sample Input: 4 1 100 175 9841

Output of Sample Input: 1 1 0 2 0 1 2 0 1 1 1 1 1 1 1 1 1 1 1 1

In this blog post, I shall go over what my proposed solution would look like in at most pseudo-code.

The first step is the understand what the problem is asking for. Given a number of nodes, K, we want to assign them into dimension containers, Xm, such that the maximum value is achieved.

For example, looking at the second sample test case, K = 100: X0 = 1, with a total weight of 1; X2 = 2, with a total weight of 18; X4 = 1, with a total weight of 81. So 1 + 18 + 81 = 100 = K. Notice that I didn't even look at the value. It's obvious that higher dimensions have greater value and so we want to fit the balls from high to low dimension to achieve the greatest value.

The problem also has a cap for K. 3^15 = 14,348,907, which exceeds the cap. Therefore, the highest dimension that a cargo ship can hold is 3^14 = 4,782,969.

So we really have 15 buckets that can be filled with i-th dimension cargo. If we see the problem as receiving K balls, then every time an i-th dimension bucket is used, 3^i balls are consumed from K as stored balls.

Starting from the highest 14th dimensional bucket, we will attempt to consume the remaining balls without going negative. If we can't, then we will go down a dimension and see if that dimension is able to consume. We will continue to consume balls until we reach zero balls. The 0-th dimension consumes single balls so we will eventually achieve this.

So here is my pseudo-code of my solution:

SolveSpaceballs(K)

# The dimensional buckets

X = X[14]

# Start at the 14th dimension

i = 14

# Track how many balls were consumed

L = K

# Loop until all balls are consumed

while L > 0

# If we can consume 3^i balls... if L <= 3^i

# Then we will use this bucket

X[i] += 1

# We consumed 3^i balls

L -= 3^i

else

# Otherwise, we will move to the next bucket down

i -= 1

END

The solution fills the buckets with the number of times it is used. We can iterate through the buckets and print out its value as per specification so that we start from the 14th dimension and skip the element until we reach a non-zero bucket to begin printing values.

So let's run the test cases:

K = 1

Loop and decrement i until i = 0 where 3^0 = 1 and increment X[0] = 1 for a final result of:

Loop and decrement i until i = 0 where 3^0 = 1 and increment X[0] = 1 for a final result of:

[1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]

Output: 1, 1, 1, 1, 1, 1, 1, 1, 1

I am curious what others think of this problem and other approaches to this problem. I thought this problem was pretty straight-forward but a common pitfall is reading too much into the i-th dimension value and the i-th dimension itself.