Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Find Good Days to Rob the Bank

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2100" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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[4]. On day 3, we have security[1] >= 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

Explanation

Here's the solution to the problem, focusing on efficiency:

  • Prefix and Suffix Arrays: Calculate two arrays: non_increasing and non_decreasing. non_increasing[i] stores the number of consecutive days ending at i where the number of guards is non-increasing. non_decreasing[i] stores the number of consecutive days starting at i where the number of guards is non-decreasing.
  • Good Day Check: Iterate through the security array. A day i is a good day if i >= time, i + time < len(security), non_increasing[i] >= time, and non_decreasing[i] >= time.
  • Optimization: Using prefix/suffix arrays avoids redundant calculations, making the solution linear.

  • Time & Space Complexity: O(n) time, O(n) space.

Code

    def goodDaysToRobBank(security, time):
    n = len(security)
    non_increasing = [0] * n
    non_decreasing = [0] * n

    for i in range(1, n):
        if security[i] <= security[i - 1]:
            non_increasing[i] = non_increasing[i - 1] + 1

    for i in range(n - 2, -1, -1):
        if security[i] <= security[i + 1]:
            non_decreasing[i] = non_decreasing[i + 1] + 1

    result = []
    for i in range(n):
        if i >= time and i + time < n and non_increasing[i] >= time and non_decreasing[i] >= time:
            result.append(i)

    return result

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Find Good Days to Rob the Bank