🔍 알고리즘/프로그래머스 Python
[Python] 프로그래머스 1845.폰켓몬 (Lv.1)
탄치
2022. 8. 7. 22:33
https://school.programmers.co.kr/learn/courses/30/lessons/1845
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
해쉬의 연습문제로 등록되어 있지만, Key값으로 쓸 이름이 숫자형태여서 그냥 boolean 배열로 관리해도 충분히 풀이할 수 있습니다.
폰켓몬을 가져갈수 있는 최대값(N/2)범위 이내에서 겹치지 않게 카운팅을 해주면 됩니다.
boolean 배열과 Dictionary 두가지 형태로 작성했습니다.
def solution(nums):
answer = 0
count = len(nums) // 2
picked = [False for i in range(200_001)]
index = 0
while answer < count and index < len(nums):
if not picked[nums[index]]:
picked[nums[index]] = True
answer += 1
index += 1
return answer
//딕셔너리 사용
def solution_with_dict(nums):
answer = 0
count = len(nums) // 2
picked = {}
index = 0
while answer < count and index < len(nums):
if not nums[index] in picked:
picked[nums[index]] = 1
answer += 1
index += 1
return answer
728x90