Open In App

Total time to get all the persons off the stairs

Last Updated : 18 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given two integer N and L and two array pos[] and direction[] each of size N, where L is the total number of stairs, pos[i] denotes that the ith person is standing at step i and direction[i] tells us the direction in which the ith person is moving. If direction[i] = 1, then ith person is moving up and if direction[i] = -1, then he is moving down. Each person is moving at a speed of 1 step per second. When 2 person meet at any step, they both start moving in the opposite direction with the same speed.

Find the time to get all the persons out of the staircase. pos[] array consists of unique values.

Examples:

Input: N= 4, L= 4, pos[]= {0, 1, 3, 4}, direction[]= {1, 1, -1, -1}
Output: 4
Explanation:

At T = 0:
Person A is at step 0 moving up.
Person B is at step 1 moving up.
Person C is at step 3 moving down.
Person D is at step 4 moving down.

At T = 1:
Persons A and B have moved one step up to steps 1 and 2 respectively.
Persons C and D have moved one step down to steps 2 and 3 respectively.
Here, persons B and C meet at step 2 and change their direction. Now, B is moving down and C is moving up.

At T = 1.5:
Person B meets withs person A between steps 1 and 2 and B starts moving up and A starts moving down, while person C meets withs person D between steps 2 and 3 and C starts moving down and D starts moving up.

At T = 2:
Persons A and D continue their movement reaching steps 1 and 3 respectively.
Persons B and C meet at step 2 and change their direction. Now, B is moving down and C is moving up.

At T = 3:
Person A has reached step 0 moving down.
Person B has reached step 1 moving down.
Person C has reached step 3 moving up.
Person D has reached step 4 moving up.

At T = 4:
Person A leaves the staircase.
Person B has reached step 0 moving down.
Person C has reached step 4 moving up.
Person D leaves the staircase.

At T = 5:
All persons have left the staircase.

The last moment when a person was on the staircase is t = 4 seconds. After that, it gets out of the staircase. So it takes 4 seconds to get all the persons out of the staircase.

Input: N= 8, L= 7, pos[]= {0, 1, 2, 3, 4, 5, 6, 7}, direction[]= {1, 1, 1, 1, 1, 1, 1, 1}
Output: 7
Explanation: Every person is moving up, so the person at index 0 needs 7 seconds to get out of the staircase.

Approach: Follow the below idea to solve this problem:

The idea is to ignore the condition that two person change their directions when they meet. Meeting of people and changing direction is irrelevant because they don’t change the final outcome. When two people meet, they swap directions but stay at the same step. It’s as if one is taking place of another. The time it takes for a person to step off the staircase is determined by their initial position and direction. A person moving down from step pos[i] takes pos[i] time to step off. A person moving up from step xi takes L – pos[i] time to step off. The answer is the maximum of all these times.

Step-by-step algorithm:

  • Initialize maxTime = 0.
  • Iterate over each person’s position. For each position:
  • If the person is moving up (direction[i] == 1),
  • Time it takes to reach the top of the staircase, is L – pos[i].
  • If this time is larger than maxTime, update maxTime.
  • If the person is moving down (direction[i] == -1),
  • Time it takes to reach the bottom of the staircase, is pos[i].
  • If this time is larger than maxTime, update maxTime.
  • Return maxTime.
C++
// C++ code
#include <bits/stdc++.h>
using namespace std;

// Function to calculate the time when the last person
// leaves the staircase
int getTime(int L, int N, vector<int>& pos,
            vector<int>& direction)
{
    // Initialize the maximum time as 0
    int maxTime = 0;

    // Iterate over each person
    for (int i = 0; i < N; i++) {
        // If the person is moving up, calculate the time to
        // reach the top
        if (direction[i] == 1)
            maxTime = max(maxTime, L - pos[i]);

        // If the person is moving down, calculate the time
        // to reach the bottom
        else
            maxTime = max(maxTime, pos[i]);
    }

    // Return the maximum time
    return maxTime;
}

int main()
{
    // Example 1
    int N = 4, L = 4;
    vector<int> pos1 = { 0, 1, 3, 4 },
                direction1 = { 1, 1, -1, -1 };
    cout << getTime(L, N, pos1, direction1) << endl;

    // Example 2
    N = 8, L = 7;
    vector<int> pos2 = { 0, 1, 2, 3, 4, 5, 6, 7 },
                direction2 = { 1, 1, 1, 1, 1, 1, 1, 1 };
    cout << getTime(L, N, pos2, direction2) << endl;

    return 0;
}
Java
// Java Code
import java.util.*;
public class staircase{

    // Function to calculate the time when the last person
    // leaves the staircase
    public static int getTime(int L, int N, int[] pos,
                              int[] direction)
    {
        // Initialize the maximum time as 0
        int maxTime = 0;

        // Iterate over each person
        for (int i = 0; i < N; i++) {
            // If the person is moving up, calculate the
            // time to reach the top
            if (direction[i] == 1)
                maxTime = Math.max(maxTime, L - pos[i]);

            // If the person is moving down, calculate the
            // time to reach the bottom
            else
                maxTime = Math.max(maxTime, pos[i]);
        }

        // Return the maximum time
        return maxTime;
    }

    public static void main(String[] args)
    {
        // Example 1
        int N = 4, L = 4;
        int[] pos = { 0, 1, 3, 4 }, direction
                                    = { 1, 1, -1, -1 };
        System.out.println(getTime(L, N, pos, direction));

        // Example 2
        N = 8;
        L = 7;
        pos = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 };
        direction = new int[] { 1, 1, 1, 1, 1, 1, 1, 1 };
        System.out.println(getTime(L, N, pos, direction));
    }
}
Python
# Python Code

# Function to calculate the time when the last person leaves the staircase


def get_time(L, N, pos, direction):
    # Initialize the maximum time as 0
    max_time = 0

    # Iterate over each person
    for i in range(N):
        # If the person is moving up, calculate the time to reach the top
        if direction[i] == 1:
            max_time = max(max_time, L - pos[i])

        # If the person is moving down, calculate the time to reach the bottom
        else:
            max_time = max(max_time, pos[i])

    # Return the maximum time
    return max_time


# Example 1
N = 4
L = 4
pos = [0, 1, 3, 4]
direction = [1, 1, -1, -1]
print(get_time(L, N, pos, direction))

# Example 2
N = 8
L = 7
pos = [0, 1, 2, 3, 4, 5, 6, 7]
direction = [1, 1, 1, 1, 1, 1, 1, 1]
print(get_time(L, N, pos, direction))
JavaScript
// Function to calculate the time when the last person
// leaves the staircase
function getTime(L, N, pos, direction) {
    // Initialize the maximum time as 0
    let maxTime = 0;

    // Iterate over each person
    for (let i = 0; i < N; i++) {
        // If the person is moving up, calculate the time to
        // reach the top
        if (direction[i] === 1)
            maxTime = Math.max(maxTime, L - pos[i]);
        // If the person is moving down, calculate the time
        // to reach the bottom
        else
            maxTime = Math.max(maxTime, pos[i]);
    }

    // Return the maximum time
    return maxTime;
}

// Example 1
let N = 4, L = 4;
let pos1 = [0, 1, 3, 4];
let direction1 = [1, 1, -1, -1];
console.log(getTime(L, N, pos1, direction1));

// Example 2
N = 8, L = 7;
let pos2 = [0, 1, 2, 3, 4, 5, 6, 7];
let direction2 = [1, 1, 1, 1, 1, 1, 1, 1];
console.log(getTime(L, N, pos2, direction2));

// This code is contributed by shivamgupta0987654321

Output
4
7

Time Complexity: O(N), where N is the size of the array.
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads