FastPrepLeaves Remaining After Wind Gusts
Problems/Leaves Remaining After Wind Gusts

Leaves Remaining After Wind Gusts

OpenAI logoOpenAI● MediumFULLTIMENEW GRADINTERNOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source provides the garden, directional gust sequence, boundary loss, and remaining-sum objective. The judged bounds, empty-sequence case, and callable interface complete the task; the core behavior is an approximately 95% match.

A rectangular garden is represented by garden. The value garden[r][c] is the number of leaves initially in cell (r, c).

Process every character of gusts from left to right. A gust shifts all leaves that are still present by one cell:

  • U: one row up
  • D: one row down
  • L: one column left
  • R: one column right

Leaves shifted outside the garden disappear permanently. Return the total number of leaves remaining after all gusts.

Function

remainingLeafSum(garden: int[][], gusts: String) → long

Examples

Example 1

garden = [[1,2,3],[4,5,6]]gusts = "R"return = 12

The rightmost column leaves the garden. The surviving original values are 1, 2, 4, and 5.

Example 2

garden = [[1,2],[3,4]]gusts = "UD"return = 7

The upward gust permanently removes the first row. The following downward gust cannot restore it, so only values 3 and 4 remain.

Example 3

garden = [[5]]gusts = "L"return = 0

The only leaves move beyond the left boundary.

Constraints

  • 1 ≤ garden.length, garden[r].length ≤ 200
  • Every row has the same length.
  • 0 ≤ garden[r][c] ≤ 10^9
  • 0 ≤ gusts.length ≤ 200000
  • gusts contains only U, D, L, and R.

More OpenAI problems

  • Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
  • Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
All OpenAI interview questionsSee OpenAI hiring insights
Saved
class Solution {
    public long remainingLeafSum(int[][] garden, String gusts) {
        // write your code here
    }
}
Checking account…