4. 완전 탐색 - 모의고사

coding by 스플릿·2021년 12월 27일

스터디

목록 보기
3/11

https://programmers.co.kr/learn/courses/30/lessons/42840
코딩테스트 - 완전탐색 - 모의고사

예외 상황

1.문제가 0개일 때는 모든 학생 리턴

Python3

if len(answers) == 0:
  return [1, 2, 3]

Java

if(answers.length == 0){ int [] ret = {1,2,3}; return ret;};

1. 학생 1,2,3이 찍는 번호의 반복되는 사이클 배열, 정답갯수 배열 생성

Python3

first = [1, 2, 3, 4, 5]
second = [2, 1, 2, 3, 2, 4, 2, 5]
third = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
count = [0,0,0]

Java

int [] first = {1, 2, 3, 4, 5};
int [] second = {2, 1, 2, 3, 2, 4, 2, 5};
int [] third = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
int [] count = {0, 0, 0};

2. 문제의 답이 문제번호를 사이클의 길이로 나눈 나머지번째 값과 같을 경우 정답 처리

Python3

for i in range(len(answers)):
  if answers[i] == first[i%5]:
    count[0] += 1
  if answers[i] == second[i%8]:
    count[1] += 1
  if answers[i] == third[i%10]:
    count[2] += 1

Java

for(int i=0;i<answers.length;i++){
  if(answers[i] == first[i%5])count[0] += 1;
  if(answers[i] == second[i%5])count[1] += 1;
  if(answers[i] == third[i%5])count[2] += 1;
}

3. count배열에서 가장 큰 값을 가진 인덱스들에다 1을 더해서 ret 배열에 추가해서 리턴

Python3

biggest = max(count);
    ret = list()
    for i in range(len(count)):
        if count[i] == biggest:
            ret.append(i+1)

Java

int highest = 0, highest_count = 1;
for( int i : count ){
	if(i > highest){
		highest = i;
		highest_count = 1;
	}
	else if(i==highest){
		highest_count += 1;
	}
}
int [] ret = new int [highest_count];
int idx = 0;
for(int i=0;i<count.length;i++){
	if(count[i]==highest)
	ret[idx++]=i+1;
}
return ret;

최종 코드

Python3

def solution(answers):
    #문제가 0개일 때는 모든 학생 리턴
    if len(answers) == 0:
        return [1, 2, 3]
    #1. 학생 1,2,3이 찍는 번호의 반복되는 사이클 배열, 정답갯수 배열 생성
    first = [1, 2, 3, 4, 5]
    second = [2, 1, 2, 3, 2, 4, 2, 5]
    third = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
    count = [0,0,0]
    #2. 문제의 답이 문제번호를 사이클의 길이로 나눈 나머지번째 값과 같을 경우 정답 처리
    for i in range(len(answers)):
        if answers[i] == first[i%5]:
            count[0] += 1;
        if answers[i] == second[i%8]:
            count[1] += 1;
        if answers[i] == third[i%10]:
            count[2] += 1;
    #3. count배열에서 가장 큰 값을 가진 인덱스들에다 1을 더해서 ret 배열에 추가해서 리턴
    biggest = max(count);
    ret = list()
    for i in range(len(count)):
        if count[i] == biggest:
            ret.append(i+1)

    return ret

JAVA

class Solution {
    public int [] solution(int[] answers) {
        //문제가 0개일 때는 모든 학생 리턴
        if(answers.length == 0){ int [] ret = {1,2,3}; return ret;};
        //1. 학생 1,2,3이 찍는 번호의 반복되는 사이클 배열, 정답갯수 배열 생성
        int [] first = {1, 2, 3, 4, 5};
        int [] second = {2, 1, 2, 3, 2, 4, 2, 5};
        int [] third = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        int [] count = {0, 0, 0};
        //2. 문제의 답이 문제번호를 사이클의 길이로 나눈 나머지번째 값과 같을 경우 정답 처리
        for(int i=0;i<answers.length;i++){
            if(answers[i] == first[i%5])count[0] += 1;
            if(answers[i] == second[i%8])count[1] += 1;
            if(answers[i] == third[i%10])count[2] += 1;
        }
        //3. count배열에서 가장 큰 값을 가진 인덱스들에다 1을 더해서 ret 배열에 추가해서 리턴
        int highest = 0, highest_count = 1;
        for( int i : count ){
            if(i > highest){
                highest = i; highest_count = 1;
            }
            else if(i==highest)
                highest_count += 1;
        }
        int [] ret = new int [highest_count];
        int idx = 0;
        for(int i=0;i<count.length;i++)
            if(count[i]==highest)
                ret[idx++]=i+1;
        return ret;
    }
}

0개의 댓글