Reach the last room of a monster graph without your energy dropping below zero

Quick Overview

Decide whether you can walk from room 0 to the last room of a directed graph when entering each room costs energy equal to its monster's health and your energy must never fall below zero. It tests turning a reachability rule into a path-cost question, choosing a graph search that stays efficient, and charging for the first and last rooms correctly.

Reach the last room of a monster graph without your energy dropping below zero

Company: Confluent

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A building has `n` rooms numbered `0` to `n - 1`. One-way doors between the rooms form a directed graph, given as a list of edges. Each room holds a monster, and `health[i]` is the health of the monster in room `i`. You start in room `0` with `energy` units of energy. Each time you enter a room you must fight its monster, which costs energy equal to the monster's health. You also fight the monster in room `0` as soon as you start. If your energy ever drops below `0`, you cannot continue. Return `True` if some path from room `0` to room `n - 1` keeps your energy at `0` or above the whole way. Return `False` otherwise. ### Function Signature ```python def can_reach_target(n: int, edges: list[list[int]], health: list[int], energy: int) -> bool: ``` ### Rules - `edges[i] = [u, v]` is a door you can walk through from room `u` to room `v`, but not from `v` back to `u`. - The fight in room `0` happens before you take any door, and the fight in room `n - 1` happens when you arrive there. Both count. - Finishing a fight with exactly `0` energy is allowed. ### Constraints - `2 <= n <= 1000` - `0 <= len(edges) <= 5000` - `edges[i] = [u, v]` with `0 <= u, v < n` - `0 <= health[i] <= 10000` - `0 <= energy <= 100000` - There are no self-loops. - The same pair of rooms may be joined by more than one edge. ### Examples ```text Input: n = 4, edges = [[0,1],[1,2],[2,3],[0,3]], health = [1,5,3,2], energy = 10 Output: True ``` Going straight from room 0 to room 3 costs 1 + 2 = 3 and leaves 7 energy. The longer path `0 -> 1 -> 2 -> 3` would cost 11 and fail, but one valid path is enough. ```text Input: n = 3, edges = [[0,1],[1,2]], health = [3,5,4], energy = 6 Output: False ``` After the fight in room 0 you have 3 energy, and entering room 1 would leave -2. Room 1 is the only way into room 2. ```text Input: n = 5, edges = [[0,1],[1,4],[0,2],[2,3],[3,4]], health = [2,9,1,1,3], energy = 8 Output: True ``` The path `0 -> 1 -> 4` uses fewer doors but costs 2 + 9 + 3 = 14. The path `0 -> 2 -> 3 -> 4` costs 2 + 1 + 1 + 3 = 7, so you reach room 4 with 1 energy left.

Overview: Decide whether you can walk from room 0 to the last room of a directed graph when entering each room costs energy equal to its monster's health and your energy must never fall below zero. It tests turning a reachability rule into a path-cost question, choosing a graph search that stays efficient, and charging for the first and last rooms correctly.

Confluent logo
Confluent
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A building has n rooms numbered 0 to n - 1. One-way doors between the rooms form a directed graph, given as a list of edges. Each room holds a monster, and health[i] is the health of the monster in room i.

You start in room 0 with energy units of energy. Each time you enter a room you must fight its monster, which costs energy equal to the monster's health. You also fight the monster in room 0 as soon as you start. If your energy ever drops below 0, you cannot continue.

Return True if some path from room 0 to room n - 1 keeps your energy at 0 or above the whole way. Return False otherwise.

Function Signature

def can_reach_target(n: int, edges: list[list[int]], health: list[int], energy: int) -> bool:

Rules

  • edges[i] = [u, v] is a door you can walk through from room u to room v , but not from v back to u .
  • The fight in room 0 happens before you take any door, and the fight in room n - 1 happens when you arrive there. Both count.
  • Finishing a fight with exactly 0 energy is allowed.

Constraints

  • 2 <= n <= 1000
  • 0 <= len(edges) <= 5000
  • edges[i] = [u, v] with 0 <= u, v < n
  • 0 <= health[i] <= 10000
  • 0 <= energy <= 100000
  • There are no self-loops.
  • The same pair of rooms may be joined by more than one edge.

Examples

Input: n = 4, edges = [[0,1],[1,2],[2,3],[0,3]], health = [1,5,3,2], energy = 10
Output: True

Going straight from room 0 to room 3 costs 1 + 2 = 3 and leaves 7 energy. The longer path 0 -> 1 -> 2 -> 3 would cost 11 and fail, but one valid path is enough.

Input: n = 3, edges = [[0,1],[1,2]], health = [3,5,4], energy = 6
Output: False

After the fight in room 0 you have 3 energy, and entering room 1 would leave -2. Room 1 is the only way into room 2.

Input: n = 5, edges = [[0,1],[1,4],[0,2],[2,3],[3,4]], health = [2,9,1,1,3], energy = 8
Output: True

The path 0 -> 1 -> 4 uses fewer doors but costs 2 + 9 + 3 = 14. The path 0 -> 2 -> 3 -> 4 costs 2 + 1 + 1 + 3 = 7, so you reach room 4 with 1 energy left.