<๋ฌธ์ ๋งํฌ>
https://school.programmers.co.kr/learn/courses/30/parts/12077
<ํ์ด ์ ๋ต>
1. ์์ฃผํ์ง ๋ชปํ ์ ์: ๋๋ช ์ด์ธ ๋๋ฌธ์ set์ ์ฐ๋ฉด ์๋๊ณ dict์ด๋ counter ์๋ฃํ์ ์จ์ผํ๋ค. hash ์๋ฃํ์ ์ ์ฐ๊ณ ๊ทธ๋ฅ list๋ zip๋ฑ์ ์จ๋ ํต๊ณผ๋๋ค.
2. ํฐ์ผ๋ชฌ: set์ ์ฐ๋ฉด ์ฝ๋ค
3. ์ ํ๋ฒํธ ๋ชฉ๋ก: ํด์ฌ๋ฅผ ์ ์ฐ๊ณ list๋ฅผ ์จ์ O(N)์ ํ์ดํ๋ค. ํด์ฌ๋ฅผ ์ด ํ์ด๋ ๋ดค๋๋ฐ ๊ทธ๋ฅ list๋ฅผ ์ฐ๋ ๊ฒ ๋ ์ฌ์ธ ๊ฒ ๊ฐ๋ค
4. ์์ฅ: defaultdict์ ํ์ฉํ๋ค. ์ ์ฒด ๊ฒฝ์ฐ์ ์๋ฅผ ํ์ ํ๋ ๊ณผ์ ์ด ์์ด์ ํด์ฌ๋ณด๋ค ์ํ ๋ฌธ์ ์ ๋ ๊ฐ๊น์ด ๋ฌธ์
5. ๋ฒ ์คํธ์จ๋ฒ: sort์ key๊ฐ์ผ๋ก lambda๋ฅผ ํ์ฉํด์ ์กฐ๊ฑด์ ๋ง๊ฒ ์ ๋ ฌํด์ฃผ๋ ๊ฒ ํต์ฌ์ด๋ค.
<์ ๋ต์ฝ๋>
# 1. ์์ฃผํ์ง ๋ชปํ ์ ์
def solution(participant, completion):
answer = ''
ans_dict = {}
hash_val = 0
for p in participant:
ans_dict[hash(p)] = p
hash_val += hash(p)
for c in completion:
hash_val -= hash(c)
answer = ans_dict[hash_val]
return answer
# 2. ํฐ์ผ๋ชฌ
def solution(nums):
N = len(nums)
nums_set = set(nums)
answer = min(N // 2, len(nums_set))
return answer
# 3. ์ ํ๋ฒํธ ๋ชฉ๋ก
def solution(phone_book):
answer = True
phone_book.sort()
for i in range(len(phone_book) - 1):
if phone_book[i] == phone_book[i + 1][:len(phone_book[i])]:
answer = False
break
return answer
# 4. ์์ฅ
from collections import defaultdict
def solution(clothes):
clothes_dict = defaultdict(int)
for c in clothes:
clothes_dict[c[1]] += 1
answer = 1
for type in clothes_dict:
answer *= (clothes_dict[type] + 1)
answer -= 1
return answer
# 5. ๋ฒ ์คํธ ์จ๋ฒ
from collections import defaultdict
def solution(genres, plays):
genre_play = defaultdict(int)
for i in range(len(genres)):
genre_play[genres[i]] += plays[i]
genres = list(enumerate(genres))
temp = list(zip(genres, plays))
temp.sort(key = lambda x : x[0][0])
temp.sort(key = lambda x:(genre_play[x[0][1]], x[1]), reverse=True)
genre_count = defaultdict(int)
answer = []
for (info, play) in temp:
if genre_count[info[1]] >= 2:
continue
answer.append(info[0])
genre_count[info[1]] += 1
return answer
'โญ Problem_Solving > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ณ ๋์ kit - ์์ ํ์ (Python/ํ์ด์ฌ) (0) | 2023.02.07 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ณ ๋์ kit - ์ ๋ ฌ (Python/ํ์ด์ฌ) (0) | 2023.02.05 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ณ ๋์ kit - ํ(Heap) (Python/ํ์ด์ฌ) (0) | 2023.02.04 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฌ์น์ฐ์ฐ (Python/ํ์ด์ฌ) (0) | 2023.01.31 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ณ ๋์ kit - ์คํ / ํ (0) | 2023.01.16 |
๋๊ธ