작은 사람부터 작은 옷부터 입히면 가장 효율적입니다. 즉, 정렬 후 tshirts을 하나씩 살펴보면서 people보다 크면 입히고, 아니면 넘기면 됩니다. 구현은 이전 문제와 같이 투포인터를 활용하면 됩니다.
위장
이 문제와 비슷한 느낌??
def solution(people, tshirts):
answer = 0
d = {}
for i in tshirts:
d[i] = d.get(i,0)+1
for j in people:
if d.get(j,0) >= 1 and (j in tshirts or j+1 in tshirts):
d[j] = d.get(i)-1
answer += 1
return answer
def solution(people, tshirts):
people.sort()
tshirts.sort()
p,t,ans = 0,0,0
while p<len(people) and t < len(tshirts):
if tshirts[t] >= people[p]:
ans +=1
p+=1
t+=1
return ans