In the Find Good Days to Rob the Bank coding problem, you are given an array of security values for a bank over days and an integer time. A day is considered a "good day" to rob the bank if:
time days before day are non-increasing.time days after day are non-decreasing.
You need to return a list of all such "good" indices.Amazon and Google use this problem to test a candidate's ability to avoid redundant calculations. A naive approach would re-check the neighbors for every single day, leading to complexity. The Dynamic Programming or Prefix Sum interview pattern is required here to solve the problem in linear time. It tests whether you can precompute data to answer range-based queries instantly.
This problem uses Precomputation (Dynamic Programming).
nonIncreasing: left[i] stores how many consecutive days ending at have been non-increasing.nonDecreasing: right[i] stores how many consecutive days starting at are non-decreasing.left (left-to-right) and right (right-to-left).left[i] >= time and right[i] >= time.security = [5, 3, 3, 3, 5, 6, 2], time = 2
time days, rather than at least time days.time days simply don't exist.Practice the "Two-Pass Precomputation" pattern. Many array problems that involve conditions on both the left and right sides of an element can be solved by running one pass in each direction and then intersecting the results.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Largest Sum of Averages | Medium | Solve | |
| Find All Good Indices | Medium | Solve | |
| Merge Operations for Minimum Travel Time | Hard | Solve | |
| Minimum Cost to Merge Stones | Hard | Solve | |
| Maximum Strength of K Disjoint Subarrays | Hard | Solve |