Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.06.20 - [PG] 모의고사 #88

Closed
suhyunsim opened this issue Jun 20, 2021 · 0 comments
Closed

21.06.20 - [PG] 모의고사 #88

suhyunsim opened this issue Jun 20, 2021 · 0 comments
Assignees
Labels
lv.1 프로그래머스 - level 1 브루트포스 완전탐색 성공 맞은 문제

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Jun 20, 2021

문제

핵심 아이디어

  • 풀긴 풀었는데 뭔가 더럽게 푼 느낌이 있다.
  • 수정할 필요가 느껴짐

어려운 점, 실수

풀이

package main.java.com.poogle.PG.Q42840;

import java.util.*;

public class Solution {
    public static int[] solution(int[] answers) {
        Queue<Integer> qOne = new LinkedList<>();
        Queue<Integer> qTwo = new LinkedList<>();
        Queue<Integer> qThree = new LinkedList<>();
        int[] lOne = {1, 2, 3, 4, 5};
        int[] lTwo = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] lThree = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        for (int k : lOne) {
            qOne.add(k);
        }
        for (int k : lTwo) {
            qTwo.add(k);
        }
        for (int k : lThree) {
            qThree.add(k);
        }
        int cntOne = 0;
        int cntTwo = 0;
        int cntThree = 0;
        for (int j : answers) {
            if (j == qOne.peek()) {
                cntOne++;
            }
            if (j == qTwo.peek()) {
                cntTwo++;
            }
            if (j == qThree.peek()) {
                cntThree++;
            }
            qOne.offer(qOne.poll());
            qTwo.offer(qTwo.poll());
            qThree.offer(qThree.poll());
        }
        Map<Integer, Integer> cnt = new HashMap<>();
        cnt.put(1, cntOne);
        cnt.put(2, cntTwo);
        cnt.put(3, cntThree);
        int maxValue = Collections.max(cnt.values());
        ArrayList<Integer> answer = new ArrayList<>();
        for (Map.Entry<Integer, Integer> c : cnt.entrySet()) {
            if (c.getValue() == maxValue) {
                answer.add(c.getKey());
            }
        }
        return answer.stream().filter(Objects::nonNull).mapToInt(i -> i).toArray();
    }
}
@suhyunsim suhyunsim added 브루트포스 완전탐색 lv.1 프로그래머스 - level 1 labels Jun 20, 2021
@suhyunsim suhyunsim added this to the 6월 3주 차 milestone Jun 20, 2021
@suhyunsim suhyunsim self-assigned this Jun 20, 2021
@suhyunsim suhyunsim added the 성공 맞은 문제 label Jun 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lv.1 프로그래머스 - level 1 브루트포스 완전탐색 성공 맞은 문제
Projects
None yet
Development

No branches or pull requests

1 participant