~๋ชฉ์ฐจ~
๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/42840?language=java
๋ฌธ์ ํด๊ฒฐ ํฌ์ธํธ
- ํ์๋ณ ํจํด์ ์ ์ฅํ ๋ฐฐ์ด ์ ์
- answer๋ฅผ ๋๋ฉด์ ํด๋นํ๋ ๋ฒํธ์ ํ์์ด ์์ฑํ ๋ต๊ณผ ๋น๊ตํด ๋ง์ผ๋ฉด ++
- Math์ .max ๋ฉ์๋ ์ฌ์ฉํ์ฌ ์ต๋๊ฐ ์ฐพ๊ธฐ
- ์ต๋๊ฐ๊ณผ ์ผ์นํ๋ ํ์์ด ๋ช๋ช ์ธ์ง ์ ์ ์์ผ๋ฏ๋ก ์ผ์นํ๋ ๋ฒํธ๋ฅผ ArrayList.add ๋ฅผ ์ด์ฉํ์ฌ ์ฝ์
- ์ผ์น ํ์์ ์์ ๋์ผํ ๋ฐฐ์ด ์์ฑ & ArrayList ๊ฐ ์ฝ์
์์ฑ ์ฝ๋
import java.util.*;
import java.lang.Math;
class Solution {
public int[] solution(int[] answers) {
int[] answer = new int[3];
int[] s1 = {1, 2, 3, 4, 5}; //5
int[] s2 = {2, 1, 2, 3, 2, 4, 2, 5}; //8
int[] s3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5}; //10
for(int i=0; i<answers.length; i++){
if(s1[i%5] == answers[i]) answer[0]++;
if(s2[i%8] == answers[i]) answer[1]++;
if(s3[i%10] == answers[i]) answer[2]++;
}
int max = Math.max(answer[0], Math.max(answer[1], answer[2]));
ArrayList<Integer> list = new ArrayList<>();
for(int i=0; i<3; i++)
if(max == answer[i])
list.add(i+1);
int[] result = new int[list.size()];
for(int i=0; i<result.length; i++)
result[i] = list.get(i);
return result;
}
}
๋์์ด ๋์ จ๋ค๋ฉด ์ข์์ ๋๋ฌ์ฃผ์ธ์๐
'๐๋ฌธ์ ํ์ด > ๐งฉProgrammers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] ํ๋ก๊ทธ๋๋จธ์ค ์ ๊ณ ๊ฒฐ๊ณผ ๋ฐ๊ธฐ | HashMap | HashSet (0) | 2023.05.06 |
---|---|
[JAVA] ํ๋ก๊ทธ๋๋จธ์ค ์์ ์ฐพ๊ธฐ | ์๋ผํ ์คํ ๋ค์ค์ฒด | ๊ณจ๋ ๋ฐํ์ ์ถ์ธก (0) | 2023.05.06 |
[JAVA] ํ๋ก๊ทธ๋๋จธ์ค ํฌ์ผ๋ชฌ | HashSet (0) | 2023.05.05 |
[JAVA] ํ๋ก๊ทธ๋๋จธ์ค ์ถ์ต ์ ์ | HashMap (0) | 2023.05.05 |
[JAVA] ํ๋ก๊ทธ๋๋จธ์ค 2016๋ (0) | 2023.05.05 |