A landscaper is building a stepped terrace wall out of two colors of stone: sand-colored blocks and moss-colored blocks. The terrace is built row by row starting from row 1: row k always uses exactly k blocks, every block in a single row is the same color, and the color must strictly alternate from one row to the next (no two consecutive rows share a color). The landscaper may choose either color for row 1, and after that the colors keep alternating for as many rows as the supply allows.
Given how many sand blocks and how many moss blocks are available, determine the maximum number of rows that can be completed, considering both possible starting colors, without ever using more blocks of a color than are available.
A single line with two integers sand and moss.
A single integer: the maximum achievable number of rows.
Example 1
Input
2 4
Expected
3
Explanation
With 2 sand blocks and 4 moss blocks, starting row 1 with moss lets the rows go moss (1 block, 3 moss left), sand (2 blocks, all sand used), moss (3 blocks, exactly the 3 moss remaining) — 3 full rows using every block. Starting with sand instead only reaches 2 rows, since row 3 would then need 3 sand blocks but only 1 was ever available. The best achievable height is 3.
Example 2
Input
10 1
Expected
2
Explanation
With 10 sand blocks and only 1 moss block, starting row 1 with moss uses the single moss block, then row 2 in sand uses 2 of the 10 sand blocks, reaching height 2; row 3 would need 3 moss blocks, but none remain. Starting with sand instead only reaches height 1, since row 2 would then need 2 moss blocks but just 1 is available. The best achievable height is 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →