LeetCode 2100: Find Good Days to Rob the Bank Solution

Master LeetCode problem 2100 (Find Good Days to Rob the Bank), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2100. Find Good Days to Rob the Bank

Problem Explanation

Explanation

To solve this problem, we can iterate through the array of security values and for each day, check if it satisfies the conditions of being a good day to rob the bank. We can do this by looking at the security values for the time days before and after the current day and ensuring that they are non-increasing and non-decreasing respectively.

We will maintain two arrays nonIncreasing and nonDecreasing to keep track of the non-increasing and non-decreasing sequences of security values. We will iterate through the security array and update these arrays accordingly. If a day satisfies the conditions, we add it to the result list.

The time complexity of this approach is O(n) where n is the number of days in the security array, and the space complexity is O(n) to store the result list.

Solution Code

import java.util.*;

class Solution {
    public List<Integer> findGoodDays(int[] security, int time) {
        List<Integer> result = new ArrayList<>();
        int n = security.length;
        if (n == 0) return result;

        int[] nonIncreasing = new int[n];
        int[] nonDecreasing = new int[n];

        for (int i = 1; i < n; i++) {
            nonIncreasing[i] = (security[i - 1] <= security[i]) ? nonIncreasing[i - 1] + 1 : 0;
        }

        for (int i = n - 2; i >= 0; i--) {
            nonDecreasing[i] = (security[i] >= security[i + 1]) ? nonDecreasing[i + 1] + 1 : 0;
        }

        for (int i = 0; i < n; i++) {
            if (nonIncreasing[i] >= time && nonDecreasing[i] >= time) {
                result.add(i);
            }
        }

        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2100 (Find Good Days to Rob the Bank)?

This page provides optimized solutions for LeetCode problem 2100 (Find Good Days to Rob the Bank) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2100 (Find Good Days to Rob the Bank)?

The time complexity for LeetCode 2100 (Find Good Days to Rob the Bank) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2100 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2100 in Java, C++, or Python.

Back to LeetCode Solutions