BackmediumArraysuncategorizedmedium

Min Scrolls Shelves Solution

Problem Statement

You are tasked with organizing a collection of ancient scrolls into storage shelves. Each shelf has a fixed capacity, denoted by capacity, which represents the maximum number of scrolls it can hold. Given the total number of scrolls, totalScrolls, determine the minimum number of shelves required to store all scrolls without exceeding the capacity of any single shelf.

The solution must account for the fact that if the total number of scrolls is not perfectly divisible by the shelf capacity, an additional shelf is required to store the remaining scrolls. This is a classic ceiling division problem where the result is the smallest integer greater than or equal to the quotient of the total scrolls divided by the capacity.

Input:

  • totalScrolls: An integer representing the total count of scrolls to be stored.
  • capacity: An integer representing the maximum number of scrolls each shelf can hold.

Output:

  • Return an integer representing the minimum number of shelves needed.
Example 1
Input
totalScrolls = 10, capacity = 3
Output
4

Explanation: Divide 10 by 3 to get 3.333... Since we cannot have a fraction of a shelf, we round up to the next integer. 3 shelves hold 9 scrolls, leaving 1 scroll for the 4th shelf. Thus, 4 shelves are required.

Example 2
Input
totalScrolls = 12, capacity = 4
Output
3

Explanation: Divide 12 by 4 to get exactly 3. Since the division is exact, no additional shelf is needed. 3 shelves can hold all 12 scrolls (4 per shelf). Thus, 3 shelves are required.

Example 3
Input
totalScrolls = 1, capacity = 5
Output
1

Explanation: Divide 1 by 5 to get 0.2. Rounding up gives 1. Even though the shelf can hold 5 scrolls, only 1 is present, so 1 shelf is sufficient.

Example 4
Input
totalScrolls = 100, capacity = 10
Output
10

Explanation: Divide 100 by 10 to get exactly 10. Each of the 10 shelves holds 10 scrolls. Thus, 10 shelves are required.

Constraints

  • 1 <= totalScrolls <= 10^9
  • 1 <= capacity <= 10^9
  • totalScrolls and capacity are integers
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Min Scrolls Shelves — Problem Statement & Solution Guide

ArraysMediumMixed
TimeO(1)
|
SpaceO(1)

Problem Description

You are tasked with organizing a collection of ancient scrolls into storage shelves. Each shelf has a fixed capacity, denoted by capacity, which represents the maximum number of scrolls it can hold. Given the total number of scrolls, totalScrolls, determine the minimum number of shelves required to store all scrolls without exceeding the capacity of any single shelf.

The solution must account for the fact that if the total number of scrolls is not perfectly divisible by the shelf capacity, an additional shelf is required to store the remaining scrolls. This is a classic ceiling division problem where the result is the smallest integer greater than or equal to the quotient of the total scrolls divided by the capacity.

Input:

- totalScrolls: An integer representing the total count of scrolls to be stored.

- capacity: An integer representing the maximum number of scrolls each shelf can hold.

Output:

- Return an integer representing the minimum number of shelves needed.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Min Scrolls Shelves"

medium

WHY DOES IT MATTER?

This pattern demonstrates how a seemingly iterative packing problem can be solved in constant time using arithmetic, a key insight for scaling algorithms and for interviewers looking for efficient solutions.

OPTIMIZATION CHALLENGE

The core insight is to avoid loops entirely by using integer division with a +capacity-1 adjustment, which eliminates the need for conditional checks or floating point rounding.

REAL-WORLD CONNECTION

In logistics, determining the number of shipping containers needed for a batch of goods uses the same ceiling division logic to avoid overpacking or underutilizing space.

Always prefer integer arithmetic over floating point for discrete counting problems; it eliminates precision issues and is faster on most CPUs.

COMPLEXITY AT A GLANCE

⏱ Time:O(1)
💾 Space:O(1)

Core Theory — Why This Approach?

The problem reduces to a classic ceiling division: given a fixed shelf capacity and a total number of scrolls, the minimum number of shelves needed is the smallest integer greater than or equal to totalScrolls divided by capacity. Naively, one might simulate placing scrolls one by one or iterate over each shelf, which would lead to O(totalScrolls) or O(numberOfShelves) time—unacceptable for large inputs where totalScrolls can be in the millions or billions. The optimal paradigm leverages integer arithmetic: compute (totalScrolls + capacity - 1) // capacity. This formula adds capacity-1 to the numerator before performing integer division, effectively rounding up without resorting to floating point operations. It guarantees constant time and constant space, making it ideal for high-performance systems and interview settings where efficiency is paramount.

Interview Questions on This Problem

Q1How would you compute the minimum number of containers needed to store a given number of items if each container has a fixed capacity?

By performing a ceiling division: (items + capacity - 1) // capacity. This ensures we round up to the next whole container when items do not divide evenly.

Q2What edge cases must you consider when implementing the ceiling division formula in a production system?

Handle capacity equal to zero (division by zero), negative values (if allowed), and potential integer overflow when adding capacity-1 to a large items count.

Q3Explain how you would optimize a solution that currently iterates over each item to determine the number of shelves required.

Replace the iterative placement logic with a direct arithmetic calculation using integer division with a ceiling adjustment, reducing time complexity from O(n) to O(1).

Examples

Example 1

Input

totalScrolls = 10, capacity = 3

Output

4

Explanation: Divide 10 by 3 to get 3.333... Since we cannot have a fraction of a shelf, we round up to the next integer. 3 shelves hold 9 scrolls, leaving 1 scroll for the 4th shelf. Thus, 4 shelves are required.

Example 2

Input

totalScrolls = 12, capacity = 4

Output

3

Explanation: Divide 12 by 4 to get exactly 3. Since the division is exact, no additional shelf is needed. 3 shelves can hold all 12 scrolls (4 per shelf). Thus, 3 shelves are required.

Example 3

Input

totalScrolls = 1, capacity = 5

Output

1

Explanation: Divide 1 by 5 to get 0.2. Rounding up gives 1. Even though the shelf can hold 5 scrolls, only 1 is present, so 1 shelf is sufficient.

Example 4

Input

totalScrolls = 100, capacity = 10

Output

10

Explanation: Divide 100 by 10 to get exactly 10. Each of the 10 shelves holds 10 scrolls. Thus, 10 shelves are required.

Constraints

  • 1 <= totalScrolls <= 10^9
  • 1 <= capacity <= 10^9
  • totalScrolls and capacity are integers

Optimal Approach & Strategy

Compute the ceiling of totalScrolls divided by capacity using integer arithmetic: (totalScrolls + capacity - 1) // capacity, which yields the minimum number of shelves in O(1) time.

Brute Force Approach

Iterate over each scroll, increment a counter for the current shelf, and when the counter reaches capacity, reset it and increment the shelf count until all scrolls are placed.

Verified Code Solutions

JavaScript Solution
Time: O(1)
function solution(totalScrolls, maxScrollsPerShelf) { return Math.ceil(totalScrolls / maxScrollsPerShelf); }

Asked in Top Tech Interviews

uncategorizedmediumnone

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.