All problems
0451MediumStringBit ManipulationSimulation

Taps To Settle A Cell Register

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1404Number of Steps to Reduce a Number in Binary Representation to One

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A particle counter holds its running total in a row of cells given as the string cells, each cell either '0' or '1', highest place value first. The leading cell is always '1', so the row carries no padding, and the total may be far larger than a machine word.

An operator brings the total down to 1 with taps of a single key. Each tap does one of two things, and the operator has no choice about which:

  • when the total is even, the tap shifts it down to half its value;
  • when the total is odd, the tap raises it by one.

Return how many taps it takes to reach a total of 1. A row that already reads 1 needs none.

Examples

Example 1

Input
cells = "10011"
Output
8

The row stands for 19. The taps run 19, 20, 10, 5, 6, 3, 4, 2, 1, which is eight taps in all.

Example 2

Input
cells = "1000"
Output
3

The row stands for 8, and three taps bring it through 4 and 2 to 1.

Example 3

Input
cells = "1111"
Output
5

The row stands for 15, and the taps run 15, 16, 8, 4, 2, 1.

Constraints

  • 1 <= cells.length <= 500
  • cells consists only of the characters '0' and '1'
  • cells[0] == '1'

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def taps_to_settle(cells: str) -> int:
Java
public int tapsToSettle(String cells)
September 7
Apply