Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #913: Cat and Mouse

In this guide, we solve Leetcode #913 Cat and Mouse in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns. The graph is given as follows: graph[a] is a list of all nodes b such that ab is an edge of the graph.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Graph, Topological Sort, Memoization, Math, Dynamic Programming, Game Theory

Intuition

The problem breaks into overlapping subproblems, so caching results prevents exponential repetition.

A carefully chosen DP state captures exactly what we need to build the final answer.

Approach

Define the DP state and recurrence, then compute states in the correct order.

Optionally compress space once the recurrence is clear.

Steps:

  • Choose a DP state definition.
  • Write the recurrence and base cases.
  • Compute states in the correct order.

Example

Input: graph = [[2,5],[3],[0,4,5],[1,4,5],[2,3],[0,2,3]] Output: 0

Python Solution

HOLE, MOUSE_START, CAT_START = 0, 1, 2 MOUSE_TURN, CAT_TURN = 0, 1 MOUSE_WIN, CAT_WIN, TIE = 1, 2, 0 class Solution: def catMouseGame(self, graph: List[List[int]]) -> int: def get_prev_states(state): m, c, t = state pt = t ^ 1 pre = [] if pt == CAT_TURN: for pc in graph[c]: if pc != HOLE: pre.append((m, pc, pt)) else: for pm in graph[m]: pre.append((pm, c, pt)) return pre n = len(graph) ans = [[[0, 0] for _ in range(n)] for _ in range(n)] degree = [[[0, 0] for _ in range(n)] for _ in range(n)] for i in range(n): for j in range(1, n): degree[i][j][MOUSE_TURN] = len(graph[i]) degree[i][j][CAT_TURN] = len(graph[j]) for j in graph[HOLE]: degree[i][j][CAT_TURN] -= 1 q = deque() for j in range(1, n): ans[0][j][MOUSE_TURN] = ans[0][j][CAT_TURN] = MOUSE_WIN q.append((0, j, MOUSE_TURN)) q.append((0, j, CAT_TURN)) for i in range(1, n): ans[i][i][MOUSE_TURN] = ans[i][i][CAT_TURN] = CAT_WIN q.append((i, i, MOUSE_TURN)) q.append((i, i, CAT_TURN)) while q: state = q.popleft() t = ans[state[0]][state[1]][state[2]] for prev_state in get_prev_states(state): pm, pc, pt = prev_state if ans[pm][pc][pt] == TIE: win = (t == MOUSE_WIN and pt == MOUSE_TURN) or ( t == CAT_WIN and pt == CAT_TURN ) if win: ans[pm][pc][pt] = t q.append(prev_state) else: degree[pm][pc][pt] -= 1 if degree[pm][pc][pt] == 0: ans[pm][pc][pt] = t q.append(prev_state) return ans[MOUSE_START][CAT_START][MOUSE_TURN]

Complexity

The time complexity is O(n3)O(n^3)O(n3), and the space complexity is O(n2)O(n^2)O(n2). The space complexity is O(n2)O(n^2)O(n2).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy