๋ฌธ์ ์ค๋ช
์ ๋น์ด๋ ๊ฒ์์ ์ฆ๊ธฐ๋ ์ค ๊ฐ์ง๊ณ ์๋ ๋ฌผ๊ฑด์ด ๋๋ฌด ๋ง์ ์ฐฝ๊ณ ์ ๋ฆฌ๋ฅผ ํ๊ธฐ๋ก ํ์ต๋๋ค. ์ ๋น์ด๊ฐ ๋ณด์ ํ ๊ฒ์ ์ ์ฐฝ๊ณ ๋ ์ฌ๋ฌ ์นธ์ผ๋ก ๋๋์ด์ ธ ์๊ณ ๊ฐ ์นธ์๋ ๋ฌผ๊ฑด๋ค์ด ๋ด๊ฒจ์์ต๋๋ค. ์ฐฝ๊ณ ๋ฅผ ์ ๋ฆฌํ ๋ฐฉ๋ฒ์ ๊ณ ๋ฏผํ๋ ์ ๋น์ด๋ ๊ฐ์ ๋ฌผ๊ฑด์ด ์ฌ๋ฌ ์นธ์ ๋๋์ด ๋ค์ด์๋ ๊ฒ์ ๋ฐ๊ฒฌํ๊ณ ์ฐ์ ๊ฐ์ ๋ฌผ๊ฑด๋ผ๋ฆฌ ์ต๋ํ ๊ฒน์ณ์๋ ๋ฐฉ์์ผ๋ก ์ฐฝ๊ณ ๋ฅผ ์ ๋ฆฌํ๊ธฐ๋ก ํ์ต๋๋ค. ์ ๋น์ด์ ์ฐฝ๊ณ ์ ๋ค์ด์๋ ๋ฌผ๊ฑด์ ์ด๋ฆ๊ณผ ๊ฐ์๋ ๋ฆฌ์คํธ ํํ๋ก ์ฃผ์ด์ง๋ฉฐ, ํ ์นธ์ ๊ฒน์ณ์ง ์ ์๋ ๋ฌผ๊ฑด์ ๊ฐ์์๋ ์ ํ์ด ์๋ค๊ณ ๊ฐ์ ํฉ๋๋ค.
์๋ฅผ ๋ค์ด ์ฐฝ๊ณ ์ ๊ฐ ์นธ์ ๋ด๊ฒจ์๋ ๋ฌผ๊ฑด์ ์ด๋ฆ์ด storage = ["pencil", "pencil", "pencil", "book"], ๊ฐ ๋ฌผ๊ฑด์ ๊ฐ์๊ฐ num = [2, 4, 3, 1] ์ด๋ผ๋ฉด ์ฐํ๊ณผ ์ฑ ์ ํ ์นธ์ ๊ฐ๊ฐ ๊ฒน์ณ ์์ ๊ฐ๋จํ๊ฒ clean_storage = ["pencil", "book"], clean_num = [9, 1]๋ก ๋ง๋ค ์ ์์ต๋๋ค.
์ฃผ์ด์ง solution ํจ์๋ ์ ๋ฆฌ๋๊ธฐ ์ ์ฐฝ๊ณ ์ ๋ฌผ๊ฑด ์ด๋ฆ์ด ๋ด๊ธด ๋ฌธ์์ด ๋ฆฌ์คํธ storage์ ๊ฐ ๋ฌผ๊ฑด์ ๊ฐ์๊ฐ ๋ด๊ธด ์ ์ ๋ฆฌ์คํธ num์ด ์ฃผ์ด์ง ๋, ์ ๋ฆฌ๋ ์ฐฝ๊ณ ์์ ๊ฐ์๊ฐ ๊ฐ์ฅ ๋ง์ ๋ฌผ๊ฑด์ ์ด๋ฆ์ return ํ๋ ํจ์์
๋๋ค. solution ํจ์๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ์๋ํ๋๋ก ํ ์ค์ ์์ ํด ์ฃผ์ธ์.
์ ์ถ๋ ฅ ์
storage | num | result |
["pencil", "pencil", "pencil", "book"] | [2, 4, 3, 1] | "pencil" |
["doll", "doll", "doll", "doll"] | [1, 1, 1, 1] | "doll" |
["apple", "steel", "leaf", "apple", "leaf"] | [5, 3, 5, 3, 7] | "leaf" |
["mirror", "net", "mirror", "net", "bottle"] | [4, 1, 4, 1, 5] | "mirror" |
์ ์ถ ๋ด์ญ
def solution(storage, num):
clean_storage = []
clean_num = []
for i in range(len(storage)):
if storage[i] in clean_storage:
pos = clean_storage.index(storage[i])
clean_num[pos] += num[i]
else:
clean_storage.append(storage[i])
clean_num.append(num[i])
# ์๋ ์ฝ๋์๋ ํ๋ฆฐ ๋ถ๋ถ์ด ์์ต๋๋ค.
max_num = max(clean_num)
answer = clean_storage[clean_num.index(max_num)]
return answer
ํ๋ก๊ทธ๋๋จธ์ค > PCCE ๊ธฐ์ถ๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/250126
ํ๋ก๊ทธ๋๋จธ์ค
SW๊ฐ๋ฐ์๋ฅผ ์ํ ํ๊ฐ, ๊ต์ก, ์ฑ์ฉ๊น์ง Total Solution์ ์ ๊ณตํ๋ ๊ฐ๋ฐ์ ์ฑ์ฅ์ ์ํ ๋ฒ ์ด์ค์บ ํ
programmers.co.kr
'programmers > Lv.0' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] [PCCE ๊ธฐ์ถ๋ฌธ์ ] 7๋ฒ / ๊ฐ์ต๊ธฐ (0) | 2025.02.26 |
---|---|
[Python] [PCCE ๊ธฐ์ถ๋ฌธ์ ] 6๋ฒ / ๊ฐ์ฑ์ (0) | 2025.02.26 |
[Python] [PCCE ๊ธฐ์ถ๋ฌธ์ ] 5๋ฒ / ์ฐ์ฑ (0) | 2025.02.26 |
[Python] [PCCE ๊ธฐ์ถ๋ฌธ์ ] 4๋ฒ / ์ ์ถ (0) | 2025.02.26 |
[Python] [PCCE ๊ธฐ์ถ๋ฌธ์ ] 3๋ฒ / ๋์ด ๊ณ์ฐ (0) | 2025.02.25 |