Solving LeetCode’s ‘Minimum Number of Operations to Move All Balls to Each Box’ in TypeScript

Chandler Hanson
4 min readJul 18, 2021
Photo by Nana Smirnova on Unsplash

In this article, we will be solving LeetCode’s Minimum Number of Operations to Move All Balls to Each Box in TypeScript. I have been learning TypeScript over the past couple weeks, so I wanted to try my hand with solving a LeetCode problem in TypeScript. I have two past articles about the basics of TypeScript. In part 1, I covered what Typescript is. Recap, TypeScript is a superset of JavaScript that adds a strong type system and object-oriented programming. This means we can specify types to different variables at the time of declaration. They will always hold the same type of data in that scope. This type checking helps to ensure our code works as expected and helps hunt down bugs and errors. In part 2, I gave an in-depth look into the core types of TypeScript. Let us jump right in.

Problem

You have n boxes. You are given a binary string boxes of length n, where boxes[i] is ‘0’ if the ith box is empty, and ‘1’ if it contains one ball.

In one operation, you can move one ball from a box to an adjacent box. Box i is adjacent to box j if abs(i — j) == 1. Note that after doing so, there may be more than one ball in some boxes.

Return an array answer of size n, where answer[i] is the minimum number of operations needed to move all the balls to the ith box.

Each answer[i] is calculated considering the initial state of the boxes.

Example

Input: boxes = "110"Output: [1,1,3]Explanation: The answer for each box is as follows:1) First box: you will have to move one ball from the second box to the first box in one operation.2) Second box: you will have to move one ball from the first box to the second box in one operation.3) Third box: you will have to move one ball from the first box to the third box in two operations, and move one ball from the second box to the third box in one operation.

Solution

Explanation

The key part to this problem is keeping track of the balls to the left and the balls to the right. We first calculate for the first box to know how many balls are to the right of it and how many steps it would take to move all of them over. Once we move to the next box, it will take one less step for all the balls on the right of our box to move to our box because we are moving closer to them. Additionally, it will take one more step for all the balls on the left of our box to move to our box because we are moving away from them.

We have our argument that has a type string. We want our result to be an array of numbers and our return value to be an array of numbers. Besides the strong typing, the TypeScript solution and the JavaScript solution are identical.

Walkthrough

So our first run through at our first box, ‘i’ = 0. There are no steps taken because we are at the correct box. We iterate through and find the amount of balls to the right of the first box and the steps it takes to move all of them to the first box.

Now we have to figure out how many steps it takes to move all the balls to the other boxes one by one. We first assign result of the current box to the current amount of steps. Then we check if the current box has a ball in it. If so, we have one less ball to our right and one more ball to our left. We add a step to every box to our left and remove one to every box to our right. We are getting one step farther away from every ball to our left, and one step closer to every ball on our right.

Big O

Time complexity — O(n)

Space complexity — O(n)

Resources

LeetCode Problem: https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/

TypeScript Part 1: https://chandler-hanson.medium.com/learning-typescript-part-1-d40742795f6c

TypeScript Part 2: https://chandler-hanson.medium.com/basics-of-typescript-part-2-f0b52b9f12cf

--

--

Chandler Hanson

Flatiron School software engineering alum. Experienced in JavaScript, React.js, Ruby, and Ruby on Rails. https://www.linkedin.com/in/chandler-hanson/