File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments