Connected Crop Garden
Updated
Problem statement
You are planting k crop types in a garden. The crops are numbered 1..k, and counts[i] is exactly how many cells crop i + 1 must get. Each crop must fill one connected area with at least one cell. Two cells are connected only when they share an edge. Touching only at a corner does not count.
The garden has one of two shapes:
- If
stackedSquaresisfalse, the garden is a rectangle withnrows andmcolumns. - If
stackedSquaresistrue, ann x nsquare sits directly on top of anm x msquare. Both squares line up on the left, at column0. Together they haven + mrows.
More than one planting may work, so return this exact one. Visit the garden cells row by row, from top to bottom, in a snake pattern:
- For a rectangle, go left to right on row
0. Then switch direction on every next row. - For stacked squares, go left to right on row
0whennis even, and right to left whennis odd. Switch direction on every next row, also when you step from the top square down to the bottom square. Each row visits only the cells of its own square.
Give the first counts[0] cells you visit to crop 1, the next counts[1] cells to crop 2, and so on. Every count is positive, and the counts add up to the size of the garden, so this always gives a valid planting.
Return the planting as an integer matrix. For a rectangle it has n rows and m columns. For stacked squares it has n + m rows and max(n, m) columns, so some matrix cells sit to the right of the smaller square, outside the garden. Put 0 in those cells. They are not planted and do not connect any crop areas. Do not change counts.
Function
plantConnectedGarden(n: int, m: int, stackedSquares: boolean, counts: int[]) → int[][]Examples
Example 1
n = 2m = 3stackedSquares = falsecounts = [2,3,1]return = [[1,1,2],[3,2,2]]Visit the top row from left to right and the bottom row from right to left. The three crop blocks contain 2, 3 and 1 cells; each block is connected.
Example 2
n = 2m = 3stackedSquares = truecounts = [5,8]return = [[1,1,0],[1,1,0],[1,2,2],[2,2,2],[2,2,2]]The top 2 x 2 square ends at (1, 0), and the lower 3 x 3 square begins at (2, 0). Crop 1 uses the first five cells, crossing that shared edge. The two zeros are outside the smaller square.
Example 3
n = 3m = 2stackedSquares = truecounts = [2,8,3]return = [[2,1,1],[2,2,2],[2,2,2],[2,3,0],[3,3,0]]The top square's side is odd, so row 0 runs right to left. Crop 1 gets the two rightmost cells of that row. Crop 2 fills the rest of the top square and then takes the first cell of the lower square. Crop 3 gets the last three cells.
Constraints
1 <= n, m <= 20.k == counts.length, and every count is positive.- The counts add up to
n*mfor a rectangle, or ton^2 + m^2for stacked squares. So1 <= k <= 800. - Row and column numbers start at zero. Every input follows these limits.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Flipping MatrixZomato / Eternal · ONSITE INTERVIEWZomato / Eternal interview questions
- Balanced Human, Model and Task AssignmentOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Best Starting Cell for Restaurant DistanceDoorDash · PHONE SCREENDoorDash interview questions
- Town Temperature Regression AnalysisCitadel · PHONE SCREENCitadel interview questions
- Minimum Row Deletions to Prevent a Target ORInMobi · ONSITE INTERVIEWInMobi interview questions
- Calculate the Max ValueAmazon · OA
- Find Min Num MovesMicrosoft · OAMicrosoft interview questions
- Server SelectionAirbnb · OAAirbnb interview questions
- Lexicographically Smallest Grid Path with One Letter ChangeInfosys · OAInfosys interview questions
- Daily Temperature By TownTwo Sigma · OATwo Sigma interview questions