Program to check robbers can rob the vault or not in Python

Suppose there are N robbers trying to rob a vault. A guard is away for G amount of time, and each robber needs a specific time to complete the robbery. At most two robbers can enter the vault simultaneously. We need to determine if all robbers can complete their task before the guard returns.

Key Constraints

  • If one robber enters the vault at time t and another exits at the same time t, they are never simultaneously inside.

  • If the guard returns at time G and a robber exits exactly at time G, the guard won't notice.

  • Maximum two robbers can be inside the vault at any given time.

Algorithm Logic

The solution uses dynamic programming to check if robbers can be scheduled optimally ?

  • If total time needed > 2*G: impossible (even with perfect overlap)
  • If total time ? G: always possible (sequential execution)
  • Otherwise: use DP to find optimal overlapping schedule

Example Walkthrough

For N=3, G=5, time=[3,5,2], the arrangement works as follows ?

  • t=0: Robber1 and Robber2 enter (Robber1 exits at t=3, Robber2 at t=5)
  • t=3: Robber3 enters as Robber1 exits (Robber3 exits at t=5)
  • All robbers finish by t=5 when the guard returns

Implementation

def solve(N, G, time):
    total_time = sum(time)
    
    # Case 1: Impossible even with perfect overlap
    if total_time > 2 * G:
        return False
    
    # Case 2: Can be done sequentially
    elif total_time <= G:
        return True
    
    # Case 3: Need optimal scheduling with overlaps
    else:
        # DP array: valid[i] = True if we can achieve i time units with first robber
        valid = [False] * (G + 1)
        valid[0] = True
        
        # For each robber, update possible time combinations
        for rob_time in time:
            for i in range(G, -1, -1):
                if i - rob_time >= 0 and valid[i - rob_time]:
                    valid[i] = True
        
        # Find maximum time achievable by first robber
        max_first_robber = max(i for i in range(len(valid)) if valid[i])
        
        # Check if remaining time fits in second robber's schedule
        if total_time - max_first_robber <= G:
            return True
        else:
            return False

# Test the function
N = 3
G = 5
time = [3, 5, 2]
result = solve(N, G, time)
print(f"Can robbers complete the heist? {result}")
Can robbers complete the heist? True

How It Works

The dynamic programming approach works by ?

  1. Initialization: Create a boolean array where valid[i] represents whether we can assign exactly i time units to the first robber
  2. DP Transition: For each robber's time, update all possible time combinations
  3. Optimization: Find the maximum time the first robber can handle
  4. Validation: Check if the remaining time fits within the guard's absence period

Time Complexity

The algorithm has O(N × G) time complexity, where N is the number of robbers and G is the guard's absence time. The space complexity is O(G) for the DP array.

Conclusion

This problem demonstrates optimal task scheduling with constraints using dynamic programming. The key insight is partitioning robbers into two groups that can work simultaneously within the time limit.

Updated on: 2026-03-26T18:13:46+05:30

168 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements