coding test - python/SAMSUNG SWT(SW역량 테스트)

삼성 SW역량테스트 기출 / 2015 하반기 1번 문제 바이러스검사 - Greedy / Python 파이썬

sillon 2024. 9. 13. 12:40
728x90
반응형

 

*문제 출처는 삼성전자, 코드트리에 있습니다.

 

삼멘 #일차

문제 제목: 바이러스 검사

문제 사이트: https://www.codetree.ai/training-field/frequent-problems/problems/virus-detector?&utm_source=clipboard&utm_medium=text

 

코드트리 | 코딩테스트 준비를 위한 알고리즘 정석

국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요.

www.codetree.ai


나의 풀이

import sys
input = sys.stdin.readline
r = int(input())
client = list(map(int,input().split()))

l_cnt, m_cnt = map(int,input().split()) # 팀장, 팀원 최대 검사

answer = r
## r = 최소 팀장 수
for c in client:
    remian_c= c - l_cnt # 팀장이 검사하고 남은 인원

    if  remian_c > m_cnt:
        answer += remian_c // m_cnt 

        if remian_c % m_cnt > 0:
            answer += 1

    elif remian_c > 0 and remian_c <= m_cnt :
        answer += 1

print(answer)

 

모범답안

# 변수 선언 및 입력
n = int(input())
customers = list(map(int, input().split()))
leader_capacity, member_capacity = \
            tuple(map(int, input().split()))


def required_member_num(customer_num):
    # 남은 고객이 없다면 검사 팀원은 필요가 없습니다.
    if customer_num <= 0:
        return 0
    
    # 정확히 나누어 떨어지는 경우라면, 몫 만큼의 인원이 필요합니다.
    if customer_num % member_capacity == 0:
        return customer_num // member_capacity
    # 나누어 떨어지지 않는 경우라면 1명이 추가적으로 더 필요합니다.
    else:
        return (customer_num // member_capacity) + 1


ans = 0

# 식당별로 필요한 최소 검사자 수를 검색합니다.
for customer_num in customers:
    # 팀장은 꼭 한 명 필요합니다.
    ans += 1
    
    # 필요한 팀원 인원수만큼 더합니다.
    ans += required_member_num(customer_num - leader_capacity)

# 출력:
print(ans)

※ 알아야 할 것

- 무난.. 브론즈 문제라 쉬운듯 탐욕법써서 몫, 나머지 기준으로 구하면된다.

 

728x90
반응형