[Algorithm] 프로그래머스_모의고사(JAVA)

2024. 12. 14. 15:51·Algorithm

https://school.programmers.co.kr/learn/courses/30/lessons/42840?language=java

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr


수포자 1,2,3의 고정값을 배열에 저장하고

int[] student1 = {1, 2, 3, 4, 5};
int[] student2 = {2, 1, 2, 3, 2, 4, 2, 5};
int[] student3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};

 

맞힌 개수를 각각 answer[0], answer[1], answer[2]에 저장해 주고

가장 정답을 많이 맞힌 max값을 구해

다시 그 max값과 일치하는 값을 list에 넣어줬다.

 

위의 내용을 아래와 같이 구현하였다.

import java.util.*;
class Solution {
    public int[] solution(int[] answers) {
        int[] answer = new int[3];
        int[] student1 = {1, 2, 3, 4, 5};
        int[] student2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] student3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        
        for(int i = 0; i < answers.length; i++){
            if(answers[i] == student1[i%5]){
                answer[0]++;
            }
            if(answers[i] == student2[i%8]){
                answer[1]++;
            }
            if(answers[i] == student3[i%10]){
                answer[2]++;
            }
        }
        int max = Math.max(Math.max(answer[0], answer[1]), answer[2]);
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i < 3; i++){
            if(max == answer[i]){
                list.add(i+1);
            }
        }

        return list.stream().mapToInt(Integer::intValue).toArray();
    }
}

'Algorithm' 카테고리의 다른 글

[Algorithm] 99클럽 코테 스터디 2일차 TIL | 백준_랜선 자르기(1654번)  (0) 2025.01.14
[Algorithm] 99클럽 코테 스터디 1일차 TIL | 백준_암기왕(2776번)  (0) 2025.01.13
[Algorithm] 프로그래머스_게임 맵 최단거리(JAVA)  (0) 2024.12.14
[Algorithm] 프로그래머스_체육복(JAVA)  (0) 2024.12.13
[Algorithm] 프로그래머스_완주하지 못한 선수(JAVA)  (0) 2024.12.13
'Algorithm' 카테고리의 다른 글
  • [Algorithm] 99클럽 코테 스터디 2일차 TIL | 백준_랜선 자르기(1654번)
  • [Algorithm] 99클럽 코테 스터디 1일차 TIL | 백준_암기왕(2776번)
  • [Algorithm] 프로그래머스_게임 맵 최단거리(JAVA)
  • [Algorithm] 프로그래머스_체육복(JAVA)
dev_ajrqkq
dev_ajrqkq
알고리즘 천재가 될 거야
  • dev_ajrqkq
    기록이 자산이다
    dev_ajrqkq
  • 전체
    오늘
    어제
    • 분류 전체보기 (145) N
      • Front-end (0)
      • Back-end (11)
        • Spring (1)
        • Java (8)
      • CS (9)
        • 데이터베이스 (5)
        • 네트워크 (4)
      • Algorithm (78) N
      • 이것저것 (0)
      • 버그잡기 (1)
      • TIL (37)
      • 후기 (1)
      • 취준 (0)
  • 블로그 메뉴

    • 링크

    • 공지사항

    • 인기 글

    • 태그

      TypeScript
      Til
      티스토리챌린지
      99클럽
      개발자취업
      오블완
      항해99
      코딩테스트준비
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.2
    dev_ajrqkq
    [Algorithm] 프로그래머스_모의고사(JAVA)
    상단으로

    티스토리툴바