자료구조&알고리즘/프로그래머스

프로그래머스 - 모의고사 (JAVA)

yong_ღ'ᴗ'ღ 2023. 7. 28. 01:04

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

 

프로그래머스

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

programmers.co.kr

 

접근 방식)

1. 수포자가 찍는 방식을 보면 숫자가 반복되는 패턴을 발견할 수 있다.

2. 이 문제에서는 수포자가 3명 밖에 안되니까 

정답 배열을 돌면서, 각각의 수포자가 맞았는지 확인해주면서 맞췄으면 count를 셀 예정이다.

3. 그 중 max값을 찾고

4. max 값을 가진 수포자의 번호를 return

 

→ 풀다 보니 3가지 방법으로 풀어봤다. (프로그래머스 제출 후 시간 비교 사진 첨부)

1) List 사용 → 반복문 돌아서 배열 변경 후 반환 

2) List 사용 → Stream 으로 배열 변경 후 반환

3) HashMap 사용

 

 

1) List 사용 → 반복문 돌아서 배열 변경 후 반환

import java.util.*;
class Solution {
    public int[] solution(int[] answers) {
        ArrayList<Integer> list = new ArrayList<>();
        String[] students = {"12345", "21232425", "3311224455"};
        int[] scores = new int[3];
        for (int i = 0; i < answers.length; i++) {
            if (answers[i] == students[0].charAt(i % students[0].length()) - '0')
                scores[0]++;
            if (answers[i] == students[1].charAt(i % students[1].length()) - '0')
                scores[1]++;
            if (answers[i] == students[2].charAt(i % students[2].length()) - '0')
                scores[2]++;
        }

        int max = scores[0];
        for (int i = 1; i < scores.length; i++)
            if (scores[i] > max)
                max = scores[i];

        for (int i = 0; i < scores.length; i++)
            if (scores[i] == max)
                list.add(i + 1);

        int[] answer = new int[list.size()];
        for (int i = 0; i < answer.length; i++)
            answer[i] = list.get(i);
        
        return answer;
    }
}

list를 반복문 돌아서 array로 변환해서 return

 

2) List 사용 → Stream 으로 배열 변경 후 반환

import java.util.*;
class Solution {
    public int[] solution(int[] answers) {
        ArrayList<Integer> list = new ArrayList<>();
        String[] students = {"12345", "21232425", "3311224455"};
        int[] scores = new int[3];
        for (int i = 0; i < answers.length; i++) {
            if (answers[i] == students[0].charAt(i % students[0].length()) - '0')
                scores[0]++;
            if (answers[i] == students[1].charAt(i % students[1].length()) - '0')
                scores[1]++;
            if (answers[i] == students[2].charAt(i % students[2].length()) - '0')
                scores[2]++;
        }

        int max = scores[0];
        for (int i = 1; i < scores.length; i++)
            if (scores[i] > max)
                max = scores[i];

        for (int i = 0; i < scores.length; i++)
            if (scores[i] == max)
                list.add(i + 1);

        return list.stream().mapToInt(i->i).toArray();
    }
}

list를 stream으로 array로 변환해서 return

 

3) HashMap 사용

import java.util.*;
class Solution {
    public int[] solution(int[] answers) {
        int[] a = {1, 2, 3, 4, 5};
        int[] b = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] c = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        HashMap<Integer, Integer> map = new HashMap<>();    // K:수포자번호, V:맞춘개수
        int[] correctCnt = new int[3];  
        
        for (int i = 0; i < answers.length; i++) {
            if (answers[i] == a[i % a.length])
                map.put(1, map.getOrDefault(1, 0) + 1);
            if (answers[i] == b[i % b.length])
                map.put(2, map.getOrDefault(2, 0) + 1);
            if (answers[i] == c[i % c.length])
                map.put(3, map.getOrDefault(3, 0) + 1);
        }
        
        // 돌면서 제일 큰 값 찾음
        int maxCorrect = -1;
        for (int i : map.keySet())
            maxCorrect = Math.max(maxCorrect, map.get(i));
        
        // 돌면서 큰 값이랑 같으면 리스트에 번호 추가 
        List<Integer> list = new ArrayList<>();
        for (int i : map.keySet()) {
            if (map.get(i) == maxCorrect)
                list.add(i);
        }
        
        // map은 순서 보장하지 않으니, 정렬
        Collections.sort(list);
        
        int[] answer = new int[list.size()];
        for (int i = 0; i < answer.length; i++)
            answer[i] = list.get(i);
        return answer;
    }
}

map 사용