All problems
0242EasyMathBinary SearchNewton's Method

Largest Square Patio

Tracked in this browser only
Write code

Trains the technique from

LeetCode 69Sqrt(x)

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 landscaper has tiles identical square paving stones on the pallet and wants to lay one solid square patio. Stones are laid whole, never cut, and any stones left over simply stay on the pallet.

Return the side length of the largest square patio that can be laid, counted in stones along one edge. Equivalently, return the largest integer side for which side * side is at most tiles.

Work it out with integer arithmetic: do not call a library square-root routine and do not use an exponentiation operator.

Examples

Example 1

Input
tiles = 27
Output
5

A patio 5 stones on a side takes 25 stones, so the pallet of 27 covers it and 2 stones stay behind.

Example 2

Input
tiles = 100
Output
10

A patio 10 stones on a side takes exactly 100 stones, so the pallet is used up and nothing is left over.

Example 3

Input
tiles = 3
Output
1

A single stone is already a square patio 1 stone on a side, and the other 2 stones stay on the pallet.

Constraints

  • 0 <= tiles <= 2^31 - 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 largest_square_side(tiles: int) -> int:
Java
public int largestSquareSide(int tiles)
September 7
Apply