-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoorman.java
68 lines (56 loc) · 1.37 KB
/
Doorman.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.io.*;
import java.util.*;
import java.text.*;
import java.util.Scanner;
public class Doorman{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int maxDiff = Integer.parseInt(str);
String queue = sc.nextLine();
String [] queueList = queue.split("");
int count = 0;
int countPeopleIn = 0;
int queueLength = queueList.length;
String tmp = "";
for(int i = 0; i < queueList.length; i++){
if(count == (-1)*maxDiff && queueList[i].equals("W")){
if((queueLength-1) == i){
break;
}
if(queueList[i+1].equals("M") ){
tmp = queueList[i];
queueList[i] = queueList[i+1];
queueList[i+1] = tmp;
}else{
break;
}
}
if(count == maxDiff && queueList[i].equals("M")){
if((queueLength-1) == i){
break;
}
if(queueList[i+1].equals("W")){
tmp = queueList[i];
queueList[i] = queueList[i+1];
queueList[i+1] = tmp;
}else{
break;
}
}
switch(queueList[i]){
case "W":
count--;
countPeopleIn++;
break;
case "M":
count++;
countPeopleIn++;
break;
default:
break;
}
}
System.out.println(countPeopleIn);
}
}