Aisha is playing a game where she controls a character to catch pancakes falling onto a stage. • The character can move left and right on a stage of length n. The character’s location : is in the range {1, 2, ….., n}. Let or by the location of the character at time t. At each second, Aisha can move the character to the left (C++1 = 2t – 1) one step, to the right 2+1 = 2t +1) one step, or stay in the same spot (3t+1 = x+). (Exceptions: can’t move left when current 2 = 1 and can’t move right when I = n.)
- The game lasts m seconds.
- Aisha knows the game really well, so she knows when and where pancakes will fall, i.e. pairs (t,p), which means at t seconds, a pancake will be at position p. You are given this, and assume lookup of this information in constant time.
The character starts at location 1 at time 0. Please design an algorithm to find out the most pancakes Aisha can catch. It should run in Omn) time – please also justify why.
Example: (t, p) sequence: (1, 1), (2, 2), (3, 4), (3, 5), (4, 3), (4,4), (5, 2)). Optimal solu- tion: 4 pancakes. Aisha can stay still for first step (21 = 1) and catch pancake, move right (22 = 2) and catch pancake, stay still (23 = 2) then move right (14 = 3) and catch pancake, and move left at (35 = 2) and catch pancake.
The correct answer and explanation is:
Algorithm
The task can be solved using dynamic programming (DP). Let dp[t][x] represent the maximum number of pancakes Aisha can catch if she is at position x at time t. We can build this DP table based on the following rules:
- Initialization: At
t = 0,dp[0][1] = 0(Aisha starts at position 1 and has not caught any pancakes). - Transition: For each
tandx, the value ofdp[t][x]depends on:- Staying at the same position (
x):dp[t][x] = max(dp[t-1][x], dp[t][x]). - Moving left (
x-1), ifx > 1:dp[t][x] = max(dp[t-1][x-1], dp[t][x]). - Moving right (
x+1), ifx < n:dp[t][x] = max(dp[t-1][x+1], dp[t][x]).
- Staying at the same position (
- Catch pancakes: If a pancake falls at position
xat timet, incrementdp[t][x]by 1. - Final result: The maximum value in the last row of the DP table (
dp[m][*]) gives the maximum number of pancakes Aisha can catch.
Implementation Steps
- Initialize a DP table of size
m+1 x n+1with zeros. - Preprocess the pancake positions for constant-time lookup using a hash table.
- Fill the DP table row by row (time step by time step).
- Return the maximum value in the last row.
Why O(mn)O(mn)?
- We iterate over
mtime steps. - For each time step, we update
npositions. - Each update involves constant-time operations (checking moves and adding pancake values). Thus, the time complexity is O(mn)O(mn).
Example Walkthrough
Given n = 5, m = 5, and pancakes at (1,1), (2,2), (3,4), (3,5), (4,3), (4,4), (5,2):
- Start at
(1, 1), catch 1 pancake. - Move to
(2, 2)in the second second, catch another pancake. - Move to
(3, 3)(not catching at(3,4)or(3,5)), then move to(4, 3)for a pancake. - Return to
(5, 2)for a total of 4 pancakes.
This algorithm ensures correctness and efficiency by systematically exploring all possible movements while avoiding redundant computations.