2100. Find Good Days to Rob the Bank LeetCode Solution
In this guide, you will get 2100. Find Good Days to Rob the Bank LeetCode Solution with the best time and space complexity. The solution to Find Good Days to Rob the Bank problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find Good Days to Rob the Bank solution in C++
- Find Good Days to Rob the Bank solution in Java
- Find Good Days to Rob the Bank solution in Python
- Additional Resources
Problem Statement of Find Good Days to Rob the Bank
You and a gang of thieves are planning on robbing a bank. You are given a 0-indexed integer array security, where security[i] is the number of guards on duty on the ith day. The days are numbered starting from 0. You are also given an integer time.
The ith day is a good day to rob the bank if:
There are at least time days before and after the ith day,
The number of guards at the bank for the time days before i are non-increasing, and
The number of guards at the bank for the time days after i are non-decreasing.
More formally, this means day i is a good day to rob the bank if and only if security[i – time] >= security[i – time + 1] >= … >= security[i] <= … <= security[i + time – 1] <= security[i + time].
Return a list of all days (0-indexed) that are good days to rob the bank. The order that the days are returned in does not matter.
Example 1:
Input: security = [5,3,3,3,5,6,2], time = 2
Output: [2,3]
Explanation:
On day 2, we have security[0] >= security[1] >= security[2] <= security[3] = security[2] >= security[3] <= security[4] <= security[5].
No other days satisfy this condition, so days 2 and 3 are the only good days to rob the bank.
Example 2:
Input: security = [1,1,1,1,1], time = 0
Output: [0,1,2,3,4]
Explanation:
Since time equals 0, every day is a good day to rob the bank, so return every day.
Example 3:
Input: security = [1,2,3,4,5,6], time = 2
Output: []
Explanation:
No day has 2 days before it that have a non-increasing number of guards.
Thus, no day is a good day to rob the bank, so return an empty list.
Constraints:
1 <= security.length <= 105
0 <= security[i], time <= 105
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2100. Find Good Days to Rob the Bank LeetCode Solution in C++
class Solution {
public:
vector<int> goodDaysToRobBank(vector<int>& security, int time) {
const int n = security.size();
vector<int> ans;
// dec[i] := the number of continuous decreasing numbers before i
vector<int> dec(n);
// inc[i] := the number of continuous increasing numbers after i
vector<int> inc(n);
for (int i = 1; i < n; ++i)
if (security[i - 1] >= security[i])
dec[i] = dec[i - 1] + 1;
for (int i = n - 2; i >= 0; --i)
if (security[i] <= security[i + 1])
inc[i] = inc[i + 1] + 1;
for (int i = 0; i < n; ++i)
if (dec[i] >= time && inc[i] >= time)
ans.push_back(i);
return ans;
}
};
/* code provided by PROGIEZ */
2100. Find Good Days to Rob the Bank LeetCode Solution in Java
class Solution {
public List<Integer> goodDaysToRobBank(int[] security, int time) {
final int n = security.length;
List<Integer> ans = new ArrayList<>();
// dec[i] := the number of continuous decreasing numbers before i
int[] dec = new int[n];
// inc[i] := the number of continuous increasing numbers after i
int[] inc = new int[n];
for (int i = 1; i < n; ++i)
if (security[i - 1] >= security[i])
dec[i] = dec[i - 1] + 1;
for (int i = n - 2; i >= 0; --i)
if (security[i] <= security[i + 1])
inc[i] = inc[i + 1] + 1;
for (int i = 0; i < n; ++i)
if (dec[i] >= time && inc[i] >= time)
ans.add(i);
return ans;
}
}
// code provided by PROGIEZ
2100. Find Good Days to Rob the Bank LeetCode Solution in Python
class Solution:
def goodDaysToRobBank(self, security: list[int], time: int) -> list[int]:
n = len(security)
dec = [0] * n # dec[i] := the number of continuous decreasing numbers before i
inc = [0] * n # inc[i] := the number of continuous increasing numbers after i
for i in range(1, n):
if security[i - 1] >= security[i]:
dec[i] = dec[i - 1] + 1
for i in range(n - 2, -1, -1):
if security[i] <= security[i + 1]:
inc[i] = inc[i + 1] + 1
return [i for i, (a, b) in enumerate(zip(dec, inc))
if a >= time and b >= time]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.