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.

So here is a programming contest problem I used recently in an interview: Spaceballs! My Alma Mater hosts programming contests and this was one of the recent problems.

In case the link breaks, here is the problem:

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.

Zero Dimension (Weight = 1, Price = 1)

First Dimension (Weight = 3, Price = 5)

Second Dimension (Weight = 9, Price = 21)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.

Each dimension adds a mesh of 3 nodes. So each dimension holds 3^i balls. So, X0 holds 3^0 = 1, X1 holds 3^1 = 3, X2, holds 3^2 = 9, X3 holds 3^3 = 27.

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:

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

Output: 1

**K = 100**

Loop and decrement i until i = 4 where 3^4 = 81 and increment X[4] = 1 leaving L = 19.

Loop and decrement i until i = 2 where 3^2 = 9 and increment X[2] = 1 leaving L = 10.

Loop but L can still be consumed by 3^2 so increment X[2] = 2 leaving L = 1.

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

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

Output: 1, 0, 2, 0, 1

**K = 175**

Loop and decrement i until i = 4 where 3^4 = 81 and increment X[4] = 1 leaving L = 94.

Loop but L can still be consumed by 3^4 so increment X[4] = 2 leaving L = 13.

Loop and decrement i until i = 2 where 3^2 = 9 and increment X[2] = 1 leaving L = 4.

Loop and decrement i until i = 1 where 3^1 = 3 and increment X[1] = 1 leaving L = 1.

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

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

Output: 2, 0, 1, 1, 1

**K = 9841**

Loop and decrement i until i = 8 where 3^8 = 6561 and increment X[8] = 1 leaving L = 3280.

Loop and decrement i until i = 7 where 3^7 = 2187 and increment X[7] = 1 leaving L = 1093.

Loop and decrement i until i = 6 where 3^6 = 729 and increment X[6] = 1 leaving L = 364.

Loop and decrement i until i = 5 where 3^5 = 243 and increment X[5] = 1 leaving L = 121.

Loop and decrement i until i = 4 where 3^4 = 81 and increment X[4] = 1 leaving L = 40.

Loop and decrement i until i = 3 where 3^3 = 27 and increment X[3] = 1 leaving L = 13.

Loop and decrement i until i = 2 where 3^2 = 9 and increment X[2] = 1 leaving L = 4.

Loop and decrement i until i = 1 where 3^1 = 3 and increment X[1] = 1 leaving L = 1.

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.

© 2020 Created by Daniel Leuck. Powered by

## You need to be a member of TechHui to add comments!

Join TechHui