0% found this document useful (0 votes)
68 views

Wings T4 Programming Java Handson

Fresco handson

Uploaded by

Krishna Rajes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Wings T4 Programming Java Handson

Fresco handson

Uploaded by

Krishna Rajes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Coding – Chairs Requirement hands on


2. import java.io.*;
3. import java.math.*;
4. import java.security.*;
5. import java.text.*;
6. import java.util.*;
7. import java.util.concurrent.*;
8. import java.util.function.*;
9. import java.util.regex.*;
10. import java.util.stream.*;
11. import static java.util.stream.Collectors.joining;
12. import static java.util.stream.Collectors.toList;
13.
14. class Result {
15.
16. /*
17. * Complete the 'minChairs' function below.
18. *
19. * The function is expected to return an
INTEGER_ARRAY.
20. * The function accepts STRING_ARRAY simulations
as parameter.
21. */
22.
23. public static List<Integer> minChairs(List<String>
simulations) {
24. // Write your code here
25. List<Integer> results = new ArrayList<>();
26. for(String simulation:simulations){
27. int Total = 0;
28. int Available = 0;
29. for(char action:simulation.toCharArray()){
30. if(action == 'C'){
31. if(Available > 0){
32. Available--;
33. }
34. else{
35. Total++;
36. }
37. }
38. else if(action =='R'){
39. Available++;
40. }
41. else if(action =='U'){
42. if(Available > 0){
43. Available--;
44. }
45. else{
46. Total++;
47. }
48. }
49. else if(action == 'L'){
50. Available++;
51. }
52. }
53.
54. results.add(Total);
55. }
56. return results;
57.
58. }
59.
60.
61.
62. }
63.
64. public class Solution {
65. public static void main(String[] args) throws
IOException {
66. BufferedReader bufferedReader = new
BufferedReader(new InputStreamReader(System.in));
67. BufferedWriter bufferedWriter = new
BufferedWriter(new
FileWriter(System.getenv("OUTPUT_PATH")));
68.
69. int simulationsCount =
Integer.parseInt(bufferedReader.readLine().trim());
70.
71. List<String> simulations = IntStream.range(0,
simulationsCount).mapToObj(i -> {
72. try {
73. return bufferedReader.readLine();
74. } catch (IOException ex) {
75. throw new RuntimeException(ex);
76. }
77. })
78. .collect(toList());
79.
80. List<Integer> result =
Result.minChairs(simulations);
81.
82. bufferedWriter.write(
83. result.stream()
84. .map(Object::toString)
85. .collect(joining("\n"))
86. + "\n"
87. );
88.
89. bufferedReader.close();
90. bufferedWriter.close();
91. }
92. }
93.

You might also like