Leetcode 2100: Find Good Days to Rob the Bank

grid47
grid47
Exploring patterns and algorithms
Apr 11, 2024 7 min read

You are planning a bank robbery with a gang of thieves. You are given a list, ‘security’, where each element represents the number of guards present on duty each day. You are also given an integer, ’time’, which represents the number of days before and after the current day that must have specific guard arrangements for it to be a good day for the robbery. A good day to rob the bank is defined as:

  1. There must be at least ’time’ days before and after the current day.
  2. The number of guards on the days before the current day must be non-increasing.
  3. The number of guards on the days after the current day must be non-decreasing. Return a list of all the days that are good days to rob the bank.
Problem
Approach
Steps
Complexity
Input: The input consists of a list 'security' representing the number of guards present on each day and an integer 'time' representing the number of days before and after the current day to consider.
Example: security = [4, 2, 3, 2, 5, 6, 1], time = 2
Constraints:
• 1 <= security.length <= 10^5
• 0 <= security[i], time <= 10^5
Output: Return a list of indices representing the days that are good days to rob the bank.
Example: Output: [2, 3]
Constraints:
Goal: The goal is to identify the days that satisfy the condition of having non-increasing guard numbers before the day and non-decreasing guard numbers after it.
Steps:
• Precompute the number of consecutive non-increasing days before each day.
• Precompute the number of consecutive non-decreasing days after each day.
• Iterate through the array and check if a day satisfies the condition of having enough non-increasing days before it and non-decreasing days after it.
Goal: The constraints ensure that the array size can be large and each value is within a specified range. The approach needs to handle up to 10^5 elements efficiently.
Steps:
• 1 <= security.length <= 10^5
• 0 <= security[i], time <= 10^5
Assumptions:
• The input list 'security' is non-empty.
• The integer 'time' is within the valid range.
Input: Example 1: security = [4, 2, 3, 2, 5, 6, 1], time = 2
Explanation: On day 2, security[0] >= security[1] >= security[2] <= security[3] <= security[4], making it a valid day to rob the bank. Similarly, day 3 also satisfies the condition.

Input: Example 2: security = [3, 2, 1, 3, 2], time = 1
Explanation: The days that meet the conditions are day 1 and day 3 because each of them has at least 1 day before and after with the required guard arrangements.

Input: Example 3: security = [1, 2, 3, 4, 5], time = 2
Explanation: No day has 2 previous days with non-increasing guards, so no days satisfy the condition.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus