Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3863567

Browse files
practice
1 parent 285377a commit 3863567

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

‎src/Prep/TrafficLightGreen.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package Prep;
2+
3+
import java.util.Scanner;
4+
5+
public class TrafficLightGreen {
6+
7+
8+
9+
public static void main(String[] args) {
10+
11+
Scanner sc = new Scanner(System.in);
12+
13+
int n = sc.nextInt();
14+
int arr[] = new int[n];
15+
int temp[] = new int[n];
16+
for (int i = 1; i <= n; i++) {
17+
char bulb = sc.next().charAt(0);
18+
19+
if (bulb == 'G')
20+
arr[i] = 0;
21+
if (bulb == 'R')
22+
arr[i] = 1;
23+
if (bulb == 'Y')
24+
arr[i] = 2;
25+
}
26+
27+
int k = sc.nextInt();
28+
int sum = 0;
29+
int a = 0, b = 0, answer = 0;
30+
for (int i = 1; i <= n; i++) {
31+
arr[i] += sum;
32+
if ((arr[i] + 1) % 3 == 0) {
33+
temp[i] = 1;
34+
answer += 1;
35+
sum += 1;
36+
} else if ((arr[i] + 2) % 3 == 0) {
37+
temp[i] = 2;
38+
answer += 2;
39+
sum += 2;
40+
} else
41+
temp[i] = 0;
42+
if (i >= k)
43+
sum = sum - temp[++a];
44+
}
45+
46+
System.out.println(answer);
47+
48+
}
49+
50+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /