haechandev
Algorithms

What I used to calculate before writing code

Revisiting old C-style Baekjoon solutions to find habits around input size, memory, iteration counts, and state lifetime.

August 19, 2026

When I studied C, I enjoyed pointers and arrays. I kept thinking about where a value lived, whether an array could be larger, and how much another recursive call would cost.

I do not remember every thought from that time, so I reopened my old Baekjoon solutions. They still showed what I tended to estimate before solving a problem.

How large is the input?
How much memory will this array need?
How many times can the loop run?
Am I calculating the same thing twice?
Must I retain the whole state, or only what comes next?

Frameworks and AI now handle much of implementation, but these questions remain.

Can I allocate two 500 × 500 arrays?

For Downhill, I allocated a map and a DP array.

int map[500][500];
int dp[500][500];

At four bytes per int, each is roughly 1 MB, about 2 MB together. The maximum input size made that reasonable. Keeping them global rather than inside a function also avoided adding large arrays to the stack during recursion.

The more interesting decision was what dp retained.

if (dp[y][x] != -1)
    return dp[y][x];

dp[y][x] = 0;

-1 means uncomputed; nonnegative values mean a computed path count. Once calculated, a cell’s result is reused.

I probably did this to avoid a time limit. Looking back, it was also a contract distinguishing state before and after computation. Caches and asynchronous state need a similar distinction: absent, in progress, or ready.

Problem: Downhill · Code: GitHub

Some results do not need to be stored in full

Robot Control needs to account for entering a row from either side. At first, a complete two-dimensional DP table seems necessary. But the result arrays were only:

int dp[1000];
int rl[1000][2];

A dp[1000][1000] table would occupy roughly 4 MB. This code retains only the previous row in dp and two directional scans for the current row in rl, around 4 KB and 8 KB.

The map itself remains in map[1000][1000]. Perhaps reading input row by row could have reduced memory further, but I did not duplicate the entire map size for DP results.

To compute the next row, do I need the history of every previous row, or just one row of final results?

The first step was to identify what must survive into the next stage. Screen state has the same question: what does the next screen actually need, rather than storing every response and intermediate state globally?

Problem: Robot Control · Code: GitHub

The size of the interval, rather than the numbers

Square-free Numbers uses large input values. Instead of storing every integer from the beginning, I marked only the required interval.

check[i - a] = 1;

Numbers from a through b become indices from zero through b - a. However large the absolute values are, check needs only the interval’s length.

The code still allocated fairly large prime and check arrays. It was not perfectly memory-efficient, but changing coordinates solved the problem of representing every large number.

Pagination, time-window queries, and requesting only the API fields a screen needs ask the same question:

What range do I actually need to handle now?

Problem: Square-free Numbers · Code: GitHub

What C left me with

Not many of these solutions explicitly use malloc and free. What remains is how I allocated global arrays, chose index origins, and retained state after recursion.

I learned more than pointer syntax: the habit of thinking about data size and location, repeated computation, and the lifetime of state.

Implementation is much faster now. Asking AI for a feature or installing a library can make things work quickly. I still return to these questions:

Where does this value remain?
How often does this work repeat?
Do I need to hold all of it now?

Understanding the structure helps me choose convenient tools more accurately. Revisiting those old C solutions made the foundations I still rely on clearer.