Sqrt(x)
Real-world version: Largest Square PatioThe statement here is our own: the same inputs, outputs and technique.
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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def largest_square_side(tiles: int) -> int:public int largestSquareSide(int tiles)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 16 test cases: 16 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's