Skip to content

Commit

Permalink
알고리즘
Browse files Browse the repository at this point in the history
  • Loading branch information
sunyeongchoi committed Jan 21, 2021
1 parent b71696f commit a8cd1c1
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 22 deletions.
27 changes: 27 additions & 0 deletions argorithm/123add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
T = int(input())


def dfs(n, number):
global cnt
if number == n:
cnt += 1
return 0
elif number > n:
return 0
dfs(n, number+1)
dfs(n, number+2)
dfs(n, number+3)


for _ in range(T):
cnt = 0
n = int(input())
dfs(n, 0)
print(cnt)







13 changes: 13 additions & 0 deletions argorithm/123add_dp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import sys

T = int(sys.stdin.readline())

dp = [0, 1, 2, 4]

for _ in range(T):
N = int(sys.stdin.readline())
for i in range(len(dp), N+1):
print(i)
dp.append(dp[i-1] + dp[i-2] + dp[i-3])
print(dp)
sys.stdout.write(str(dp[N]) + "\n")
32 changes: 10 additions & 22 deletions argorithm/best_album.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
from collections import defaultdict
from operator import itemgetter


def solution(genres, plays):
answer = []
# 장르별 재생횟수 합계 구한 뒤 내림차순 정렬
genre_play_dict = defaultdict(lambda: 0)
print(genre_play_dict)
for genre, play in zip(genres, plays):
genre_play_dict[genre] += play
print(genre_play_dict)

genre_rank = [genre for genre, play in sorted(genre_play_dict.items(), key=itemgetter(1), reverse=True)]
print(genre_rank)
sorted_genre_play_dict = sorted(genre_play_dict.items(), key=lambda x: x[1], reverse=True)

final_dict = defaultdict(lambda: [])
print(final_dict)
for i, genre_play_tuple in enumerate(zip(genres, plays)):
final_dict[genre_play_tuple[0]].append((genre_play_tuple[1], i))
print(final_dict)

answer = []
for genre in genre_rank:
one_genre_list = sorted(final_dict[genre], key=itemgetter(0), reverse=True)
print(one_genre_list)
if len(one_genre_list) > 1:
answer.append(one_genre_list[0][1])
answer.append(one_genre_list[1][1])
else:
answer.append(one_genre_list[0][1])
# 장르별 재생횟수 내림차순
genre_play_list_dict = defaultdict(lambda: [])
for index, genre_play_tuple in enumerate(zip(genres, plays)):
genre_play_list_dict[genre_play_tuple[0]].append((index, genre_play_tuple[1]))

return answer
for genre in sorted_genre_play_dict:
answer += (sorted(genre_play_list_dict[genre[0]], key=lambda x: x[1], reverse=True)[:2])
return [x[0] for x in answer]


if __name__ == '__main__':
Expand Down
16 changes: 16 additions & 0 deletions argorithm/best_album_others.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def solution(genres, plays):
answer = []
d = {e: [] for e in set(genres)}
print(d)
for e in zip(genres, plays, range(len(plays))):
d[e[0]].append([e[1], e[2]])
genreSort =sorted(list(d.keys()), key=lambda x: sum( map(lambda y: y[0], d[x])), reverse=True)
for g in genreSort:
temp = [e[1] for e in sorted(d[g], key=lambda x: (x[0], -x[1]), reverse=True)]
answer += temp[:min(len(temp), 2)]
return answer


if __name__ == '__main__':
real_answer = solution(['classic', 'pop', 'classic', 'classic', 'pop'], [500, 600, 150, 800, 2500])
print(real_answer)
35 changes: 35 additions & 0 deletions argorithm/best_album_practice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from collections import defaultdict
from operator import itemgetter


def solution(genres, plays):
genre_play_dict = defaultdict(lambda: 0)
for genre, play in zip(genres, plays):
genre_play_dict[genre] += play

genre_rank = [genre for genre, play in sorted(genre_play_dict.items(), key=itemgetter(1), reverse=True)]
print(genre_rank)

final_dict = defaultdict(lambda: [])
print(final_dict)
for i, genre_play_tuple in enumerate(zip(genres, plays)):
print(genre_play_tuple)
final_dict[genre_play_tuple[0]].append((genre_play_tuple[1], i))
print(final_dict)

answer = []
for genre in genre_rank:
one_genre_list = sorted(final_dict[genre], key=itemgetter(0), reverse=True)
print(one_genre_list)
if len(one_genre_list) > 1:
answer.append(one_genre_list[0][1])
answer.append(one_genre_list[1][1])
else:
answer.append(one_genre_list[0][1])

return answer


if __name__ == '__main__':
real_answer = solution(['classic', 'pop', 'classic', 'classic', 'pop'], [500, 600, 150, 800, 2500])
print(real_answer)

0 comments on commit a8cd1c1

Please sign in to comment.