일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 우유가 옆으로 넘어지면 아야
- Have a good day :)
- SSAFY 10기 화이팅
- 텐션 업 10기 화이팅
- SSAFY 테스트
- 우유아야
- BFS
- 모르고리즘
- 코로나 싫어요
- SSAFY IM/A
- SeongSeobDang
- DP
- Java 환경 설정
- SSAFY 화이팅
- 아자아자 화이팅
- DFS
- 텐션 업 10기!
- 네트워크
- LeetCode #릿코드 #좋은 하루 되세요 #Have a nice day
- I am Korean
- 자고 싶다
- Have a nice day.
- 우유가옆으로넘어지면아야
- 수학
- HAVE A GOOD DAY
- amazon
- 자료구조
- have a nice day
- Hamming weight
- Today
- Total
Hope Everyone Is Happy
[Hard] 2534. Time Taken to Cross the Door(Java) 본문
[Hard] 2534. Time Taken to Cross the Door(Java)
J 크 2023. 12. 15. 15:49Time 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 :)
'※ 릿코드 ( LeetCode ) > [Java] 문제 풀이 ( Solve the problems)' 카테고리의 다른 글
[Hard] 818. Race Car (Java) (0) | 2023.12.18 |
---|---|
[Hard] 1293. Shortest Path in a Grid with Obstacles Elimination (Java) (2) | 2023.12.17 |
[Hard] 489. Robot Room Cleaner (Java) (0) | 2023.12.11 |
[Medium] 419. Battleships in a Board (Java) (3) | 2023.12.05 |
[Medium] 366. Find Leaves of Binary Tree (Java) (0) | 2023.12.04 |