99클럽 코테 스터디 20일차 TLI - (완전탐색)

김재령·2024년 11월 18일
0

코테

목록 보기
23/38
post-thumbnail

문제 : https://school.programmers.co.kr/learn/courses/30/lessons/42840

🚨오늘의 학습

⭐️완전탐색⭐️

모든 경우의 수를 시도하여 정답을 찾는 방법론입니다.
알고리즘이 아니므로 구현이 정해져 있지 않다

🗝️ 각 수험자별 찍기 패턴 파악
찍기 패턴의 반복 → 🔅% 계산

🗝️ 배열의 정렬
1. stream.sort()

//오름차순 정렬
int[] sortedAsc = Arrays.stream(arr).sort().toArray();
// 내림차순 정렬
int[] sortedArrDesc = Arrays.stream(arr)
                             .boxed()  // int[]를 Integer[]로 변환
                             .sorted(Comparator.reverseOrder())  // 내림차순 정렬
                             .mapToInt(Integer::intValue)  // 다시 int[]로 변환
                             .toArray();


import java.util.*

public class P_math_test2 {

    static int[] first = {1,2,3,4,5};
    static int[] second = {2,1,2,3,4,2,5};
    static int[] third = {3,3,1,1,2,2,4,4,5,5};
    

    public static void main(String[] args) {

        int[] result = solution(new int[]{1,3,2,4,2});
        Arrays.stream(result).forEach(System.out::print);

    }



    public static int[] solution(int[] answers) {

        int firstCnt = 0;
        int secondCnt = 0;
        int thirdCnt = 0;

        for (int i = 0; i < answers.length; i++) {
            if (answers[i] == first[i % first.length])
                firstCnt++;
            if (answers[i] == second[i % second.length])
                secondCnt++;
            if (answers[i] == third[i % third.length])
                thirdCnt++;
        }

        Map<Integer, Integer> map = new HashMap<>();
        map.put(1,firstCnt);
        map.put(2,secondCnt);
        map.put(3,thirdCnt);

       int maxPoint = map.entrySet().stream().mapToInt(Entry::getValue).max().getAsInt();
       return map.entrySet().stream().filter(e -> e.getValue()==maxPoint).mapToInt(Entry::getKey).toArray();
       
    }
}
profile
with me

0개의 댓글