11import java .util .*;
22class Solution {
3- public int [] solution (int [] answers ) {
4- int [] score = new int [ 3 ];
5-
6- int [] pattern1 ={ 1 , 2 , 3 , 4 , 5 };
7- int l1 = pattern1 . length ;
8- int [] pattern2 ={ 2 , 1 , 2 , 3 , 2 , 4 , 2 , 5 } ;
9- int l2 = pattern2 . length ;
10- int [] pattern3 ={ 3 , 3 , 1 , 1 , 2 , 2 , 4 , 4 , 5 , 5 } ;
11- int l3 = pattern3 . length ;
12-
13- // 6 -> 1, 4, 2
14- for ( int i = 0 ; i < answers . length ; i ++){
15- if (answers [ i ] == pattern1 [ i % l1 ] ){
16- score [ 0 ] ++;
3+ public int [] solution (int [] answers ) {
4+ int [] person1 = { 1 , 2 , 3 , 4 , 5 }; // 5
5+ int [] person2 = { 2 , 1 , 2 , 3 , 2 , 4 , 2 , 5 }; // 8
6+ int [] person3 = { 3 , 3 , 1 , 1 , 2 , 2 , 4 , 4 , 5 , 5 }; // 10
7+
8+ int totalCnt = answers . length ;
9+ int cnt = 0 ;
10+ int score1 = 0 ;
11+ int score2 = 0 ;
12+ int score3 = 0 ;
13+ while ( cnt < totalCnt ) {
14+ int ans = answers [ cnt ];
15+ if (ans == person1 [ cnt % 5 ] ){
16+ score1 ++;
1717 }
18- if (answers [ i ] == pattern2 [ i % l2 ] ){
19- score [ 1 ] ++;
18+ if (ans == person2 [ cnt % 8 ] ){
19+ score2 ++;
2020 }
21- if (answers [ i ] == pattern3 [ i % l3 ] ){
22- score [ 2 ] ++;
21+ if (ans == person3 [ cnt % 10 ] ){
22+ score3 ++;
2323 }
24+ cnt ++;
2425 }
25- List < Integer > answer = new ArrayList <>();
26- int maxx =( int ) Math . max ( score [ 0 ], Math . max ( score [ 1 ], score [ 2 ]) );
27-
28- if (maxx == score [ 0 ] ){
26+
27+ ArrayList < Integer > answer = new ArrayList <>( );
28+ int max = ( int ) Math . max ( Math . max ( score1 , score2 ), score3 );
29+ if (max == score1 ){
2930 answer .add (1 );
3031 }
31- if ( maxx == score [ 1 ]) {
32+ if ( max == score2 ) {
3233 answer .add (2 );
3334 }
34- if ( maxx == score [ 2 ]) {
35+ if ( max == score3 ) {
3536 answer .add (3 );
36- }
37-
38- int [] answer2 =new int [answer .size ()];
39- for (int i =0 ;i < answer .size ();i ++){
40- answer2 [i ]=answer .get (i );
41- }
42-
43-
44- return answer2 ;
37+ }
38+
39+ return answer .stream ().mapToInt (Integer ::intValue ).toArray ();
4540 }
4641}
0 commit comments