유형 : 구현 (그리디?)
풀이 시간 : 10분
1. 나의 풀이
def solution(A,B):
answer = 0
A.sort()
B.sort(reverse = True)
res = []
for i in range(len(A)):
res.append(A[i] * B[i])
return sum(res)
2, map을 사용한 풀이
def solution(A,B):
return sum(map(lambda a, b : a*b, sorted(A), sorted(B, reverse = True)))
map (함수, iterable)
3. zip을 이용한 풀이
두 개의 list를 묶을 때 zip을 사용할 수 있다.
def solution(A,B):
return sum([a * b for a, b in zip(sorted(A), sorted(B, reverse = True))])
'프로그래머스 > Lv. 2' 카테고리의 다른 글
6. 전화번호 목록 ★★ (hash table, startswith) (0) | 2024.07.17 |
---|---|
5. 숫자의 표현 ★ (스킬 체크 lv2) (0) | 2024.07.17 |
4. 이진 변환 반복하기 (bin, oct, hex) (0) | 2024.07.17 |
3. JadenCase 문자열 만들기 (capitalize, title) (0) | 2024.07.17 |
1. 올바른 괄호 (stack) (0) | 2024.07.17 |