Leaves Remaining After Wind
Updated
Problem statement
A rectangular grid stores a nonnegative number of leaves in each cell. For this exercise, assume each character in winds moves every remaining leaf exactly one cell in that direction: U up, D down, L left and R right.
All leaves move simultaneously for each character. A leaf that moves outside the grid is permanently removed. Process the wind characters in order and return the total number of leaves still inside the grid. An empty wind string leaves the grid unchanged.
Function
remainingLeaves(grid: int[][], winds: String) → longExamples
Example 1
grid = [[1,2],[3,4]]winds = "R"return = 4Only leaves initially in the left column survive, for a total of 1 + 3 = 4.
Example 2
grid = [[1,2],[3,4]]winds = "RL"return = 4The second wind moves surviving leaves back, but removed leaves do not return; the total is still 4.
Example 3
grid = [[5]]winds = ""return = 5No wind removes any leaf.
Constraints
1 <= grid.length, grid[0].length <= 30; every row has the same length.0 <= grid[r][c] <= 10^9.0 <= winds.length <= 2000; characters areU,D,LorR.- Return the exact total as a signed
64-bit integer.
More Cresta interview questions
- Decode Words with Anagram KeysOA · Seen Oct 2026
- Find a File in a Directory SnapshotOA · Seen Oct 2026
- Maximum Suffix–Prefix OverlapOA · Seen Oct 2026
- Particle Moves to TargetOA · Seen Jul 2026
- Rightward Bacteria ConsumptionOA · Seen Jul 2026
- Top Three Companies by ProfitOA · Seen Jul 2026
- Peak Chat Load Across AgentsPHONE SCREEN · Seen Nov 2026
- File Storage ServicePHONE SCREEN · Seen Nov 2026
- Dynamic Least-Loaded Job DispatcherPHONE SCREEN · Seen Oct 2026
- Feature Flag Evaluation EngineONSITE INTERVIEW · Seen Jul 2026
Similar questions at other companies
- Payment Partner RoutingStripe · OAStripe interview questions
- Bank Accounts with Balance HistoryMeta · OAMeta interview questions
- Capping Portfolio WeightsCapstone Investment Advisors · OA
- Repeated Mirror XORMathWorks · OAMathWorks interview questions
- Stacked Item and Category DiscountsPalantir · OAPalantir interview questions
- Stabilize Student Test ScoresVirtu Financial · OAVirtu Financial interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Minimize Task Resource ConsumptionNVIDIA · OANVIDIA interview questions
- Index of the Maximum After Left RotationsChubb · OAChubb interview questions
- Rolling MetricsAkuna Capital · OAAkuna Capital interview questions