[Hard] 2534. Time Taken to Cross the Door(Java)
Time Taken to Cross the Door - LeetCode
Can you solve this real interview question? Time Taken to Cross the Door - Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
12월 절반 is taken,,,
※ Question Summary
▶ There are n people numbered from 0 to n-1 and a door
▶ Each person can enter or exit through the door once, taking one second
▶ Given a non-decrising integer array 'arrival' of size n which has the arrivial time
▶ Given an array 'state' of size n which has the 0 (person wants to enter) or 1 (person wants to exit)
▶ if two more people want to use the door at the same time, they follow the following rules
- If the door was not used in the previous second, then the person who wants to exit goes first.
- If the door was used in the previous second for entering, the person who wants to enter goes first.
- If the door was used in the previous second for exiting, the person who wants to exit goes first.
- If multiple persons want to go in the same direction, the person with the smallest index goes first.
▶ return an array 'answer' of size n where answer[i] is the second at which the ith person crosses the door
◈ Constraints
- n == arrival.length == state.length
- 1 <= n <= 105
- 0 <= arrival[i] <= n
- arrival is sorted in non-decreasing order.
- state[i] is either 0 or 1.
◈ Input - 1
arrival = [0,1,1,2,4], state = [0,1,0,0,1]
◈ Output - 1
[0,3,1,2,4]
◈ Input - 2
arrival = [0,0,0], state = [1,0,1]
◈ Output - 2
[0,2,1]
◎ HOW TO SOLVE IT
▶ Create the class 'Person' which has the second and the number of the person
▶ Create the two Queue structure for save the people who wants to exit or enter the door
▶ Check the previous state from door, to follow the rules when people want to use the door at the same time
▶ Check the second to check the person(people) who can use the door
▶ Check with the function peek, and use the poll() if person can use the door until Queues are empty
▶ Save the person's time with the array 'result'
class Person {
int m_nSecond;
int m_nNum;
Person(){}
Person(int nSecond, int nNum) {
m_nSecond = nSecond;
m_nNum = nNum;
}
}
class Solution {
public int[] timeTaken(int[] arrival, int[] state) {
int[] narrResult = new int[arrival.length];
// state 1 : exit
// state 0 : enter
Queue<Person> exitQ = new LinkedList<>();
Queue<Person> enterQ = new LinkedList<>();
for(int i = 0 ; i < arrival.length; i++) {
if(state[i] == 0)
enterQ.add(new Person(arrival[i], i));
else
exitQ.add(new Person(arrival[i], i));
}
int nExit = 2;
int nSecond = 0;
while(!exitQ.isEmpty() && !enterQ.isEmpty() ) {
Person P = new Person();
if(exitQ.peek().m_nSecond <= nSecond && enterQ.peek().m_nSecond <= nSecond) {
if(nExit != 0) {
P = exitQ.poll();
nExit = 1;
} else {
P = enterQ.poll();
nExit = 0;
}
narrResult[P.m_nNum] = nSecond;
} else if(exitQ.peek().m_nSecond <= nSecond) {
P = exitQ.poll();
nExit = 1;
narrResult[P.m_nNum] = nSecond;
} else if(enterQ.peek().m_nSecond <= nSecond) {
P = enterQ.poll();
nExit = 0;
narrResult[P.m_nNum] = nSecond;
} else {
// exit goes first since nothing happened before
nExit = 1;
}
nSecond++;
}
// add the left people
while(!exitQ.isEmpty()) {
if(exitQ.peek().m_nSecond <= nSecond) {
Person P = exitQ.poll();
narrResult[P.m_nNum] = nSecond;
}
nSecond++;
}
while(!enterQ.isEmpty()) {
if(enterQ.peek().m_nSecond <= nSecond){
Person P = enterQ.poll();
narrResult[P.m_nNum] = nSecond;
}
nSecond++;
}
return narrResult;
}
}
I HOPE YOUR DAY GOES WELL :)