๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/Problem Solving

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ตœ์†Ÿ๊ฐ’ ๋งŒ๋“ค๊ธฐ(Lv.2) - ํŒŒ์ด์ฌ(Python)

by Jay Din 2024. 9. 4.
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/12941

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

ํ’€์ด

A์™€ B ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ์„ ๋•Œ ๋‘ ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ๊ฐ ํ•˜๋‚˜์”ฉ ์›์†Œ๋ฅผ ๋ฝ‘์•„ ๊ณฑํ•œ ๊ฐ’์˜ ์ตœ์†Œ ๋ˆ„์ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

ํฐ ์ˆ˜์™€ ์ž‘์€ ์ˆ˜๋ฅผ ๊ณฑํ•˜๋ฉด ์ตœ๋Œ€ํ•œ ์ž‘์€ ์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—

A๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ, B๋Š” ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ๊ฐ ์ž๋ฆฌ์ˆ˜๋ฅผ ๊ณฑํ•˜์—ฌ ๋”ํ•ด์ค๋‹ˆ๋‹ค.

def solution(A,B):
    answer = 0
    
    A.sort()				# ์˜ค๋ฆ„์ฐจ์ˆœ
    B.sort(reverse = True)	# ๋‚ด๋ฆผ์ฐจ์ˆœ
    
    for i in range(len(A)):
        answer += A[i]*B[i]

    return answer

 

 

728x90
๋ฐ˜์‘ํ˜•