Scroll indicator done
728x90

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


조건
  • 시험은 최대 10,000 문제로 구성되어있습니다.
  • 문제의 정답은 1, 2, 3, 4, 5중 하나입니다.
  • 가장 높은 점수를 받은 사람이 여럿일 경우, return하는 값을 오름차순 정렬해주세요.
알고리즘

찍는 방식 배열을 반복하여 문제 답과 비교하기 위해 배열이 끝까지 비교됐다면 다시 처음부터 비교 (나머지 연산으로)

Arrays.*stream*(cnt).max().getAsInt()
list.stream().mapToInt(Integer::intValue).toArray() : int Stream 사용하여 Integer list 를 int array 로 반환하기
stream 의 mapToInt 메소드로 IntStream 을 반환받으면 int 형으로 이루어진 stream이 생성됨 (Integer class의 int value 반환하는 메소드를 인자로 받음)
생성된 int형 stream 을 array로 변경 (toArray) 함
getAsInt() : 정수를 얻는

package com.example.javaproject3.psstudy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution42840 {
    int[] arr1 = new int[]{1, 2, 3, 4, 5};
    int[] arr2 = new int[]{2, 1, 2, 3, 2, 4, 2, 5};
    int[] arr3 = new int[]{3, 3, 1, 1, 2, 2, 4, 4, 5, 5};

    public int[] solution(int[] answers) {
        int[] cnt = new int[3];
        for (int i = 0; i < answers.length; i++) {
            cnt[0] += answers[i] == arr1[i % arr1.length] ? 1 : 0;
            cnt[1] += answers[i] == arr2[i % arr2.length] ? 1 : 0;
            cnt[2] += answers[i] == arr3[i % arr3.length] ? 1 : 0;
        }

        int maxNum = Arrays.stream(cnt).max().getAsInt();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            if (maxNum == cnt[i]) {
                list.add(i + 1);
            }
        }

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

    public static void main(String[] args) {
        Solution42840 solution42840 = new Solution42840();
        solution42840.solution(new int[]{1, 2, 3, 4, 5});
    }
}

 

728x90