Find Good Days to Rob the Bank - Problem
You and your gang are planning to rob a bank! π¦ To ensure a successful heist, you need to carefully analyze the security patterns over time.
You're given an array security where security[i] represents the number of guards on duty on day i. You also have a parameter time that defines your observation window.
A day is considered "good" for robbing if:
- There are at least
timedays before AND after this day (so you have enough data) - For the
timedays leading up to this day, security is non-increasing (guards are getting lazy or leaving) - For the
timedays after this day, security is non-decreasing (guards are ramping back up)
In other words, you want to rob when security is at a local minimum with consistent patterns on both sides!
Goal: Return all the good days to rob the bank (0-indexed). The order doesn't matter.
Input & Output
example_1.py β Basic Valley Pattern
$
Input:
security = [5,3,3,3,5,6,2], time = 2
βΊ
Output:
[2, 3]
π‘ Note:
Day 2: Security goes 5β3β3β3β5β6 (decreasing then increasing). Day 3: Security goes 3β3β3β3β5β6 (non-increasing then non-decreasing). Both satisfy the 2-day time window requirement.
example_2.py β No Valid Days
$
Input:
security = [1,1,1,1,1], time = 0
βΊ
Output:
[0, 1, 2, 3, 4]
π‘ Note:
With time=0, we only need to check the day itself. Since all days have equal security (satisfying both non-increasing and non-decreasing with length 0), all days are valid.
example_3.py β Insufficient Length
$
Input:
security = [1,2,3,4,5,6], time = 2
βΊ
Output:
[]
π‘ Note:
The array is strictly increasing, so no day has a non-increasing period before it (except the edges, but they don't have enough buffer days). No valid robbery days exist.
Constraints
- 1 β€ security.length β€ 105
- 0 β€ security[i], time β€ 105
- The array represents guard counts per day
- Days are 0-indexed
Visualization
Tap to expand
Understanding the Visualization
1
Map the Terrain
Security levels form peaks and valleys over time
2
Find Downward Slopes
Count consecutive decreasing security days
3
Find Upward Slopes
Count consecutive increasing security days
4
Identify Valley Floors
Days where both slopes meet the time requirement
Key Takeaway
π― Key Insight: Instead of checking each day's neighborhood repeatedly, build a map of all slope directions first, then quickly identify where stable valleys occur!
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code