-
[프로그래머스] 완전 탐색 - 모의고사알고리즘 공부/문제 풀이 2020. 8. 14. 20:21
static int[] solution(int[] answers) { int[] answer = {}; 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}; int a=0,b=0,c=0; // 배열 index int aa=0,bb=0,cc=0; // 정답 개수 for(int i=0;i<answers.length;i++) { int val=answers[i]; //정답 확인 if(A[a]==val) aa++; if(B[b]==val) bb++; if(C[c]==val) cc++; //인덱스 증가 a = (a+1)%5; b = (b+1)%8; c = (c+1)%10; } int max = Math.max(Math.max(aa, bb),cc); // max값 구하기 ArrayList<Integer> list = new ArrayList<Integer>(); if(max==aa) list.add(1); //max값이랑 같으면 넣는다. if(max==bb) list.add(2); if(max==cc) list.add(3); answer = new int[list.size()]; for(int i =0; i<answer.length; i++) { answer[i] = list.get(i); } return answer; }
문제 구현보다 오히려 출력을 더 고민한 문제
'알고리즘 공부 > 문제 풀이' 카테고리의 다른 글
[프로그래머스] 완전 탐색 - 소수 찾기 (1) 2020.08.16 [프로그래머스] 완전탐색 - 카펫 (1) 2020.08.16 [프로그래머스] 힙 - 디스크 컨트롤러 (1) 2020.08.12 [프로그래머스] 힙 - 더 맵게 (1) 2020.08.11 [프로그래머스] 스택/큐 - 프린터 (1) 2020.08.11