728x90
from collections import defaultdict
사용법..알기
오늘도 결국 문법에서 막혔다!
🌼내가 푼 문제 풀이🌼
>> 시간초과 떴다...ㅠ
이용자의 id가 담긴 문자열 배열 : id_list
각 이용자가 신고한 이용자의 id 정보가 담긴 문자열 배열 : report
정지기준이 되는 신고 횟수 : k
각 유저별로 처리결과를 메일받은 횟수 : answer
def solution(id_list, report, k):
report = list(set(report)) #중복 신고건 제거
reported_cnt = [0] * len(id_list) #신고당한횟수
report_notc_user = [] #신고당한유저
result = [0] * len(id_list) #알림수
#신고당한횟수 구하는 로직
for i in range(len(report)) :
temp1 = report[i].split(" ")
temp2 = temp1[1]
idx = id_list.index(temp2)
reported_cnt[idx] += 1
#신고당한유저 중 K번 이상 신고당한 유저 구하기
for i in range(len(reported_cnt)) :
if reported_cnt[i] >= k :
report_notc_user.append(id_list[i])
#신고당한유저를 값으로 가지고 있는 User찾기
for i in range(len(report_notc_user)) :
for j in range(len(report)) :
user = report_notc_user[i]
temp1 = report[j].split(" ")
temp2 = temp1[1]
if temp2 == user :
report_person = temp1[0]
idx=id_list.index(report_person)
result[idx] += 1
return result
🌼Hint !! 🌼
from collections import defaultdict
defaultdict 를 사용해서 풀기!!
defaultdict의 종류는 이렇게 있다.
- defaultdIct(int)
- defaultdict(list)
- defaultdict(set)
Key와 Value값으로 들어간다고 보면 됨.!
int는 지정하지 않으면 0으로 들어가고
list는 지정하지 않으면 []으로 들어가고
set은 지정하지 않으면 {}으로 들어감
from collections import defaultdict
def solution(id_list, report, k):
report = list(set(report))
cnt = defaultdict(int)
user = defaultdict(set)
answer = []
for i in report :
a,b = i.split(" ")
cnt[b] += 1
user[a].add(b)
for i in id_list :
result = 0
for j in user[i] :
if cnt[j] >= k :
result += 1
answer.append(result)
return answer
문법적으로 약한것 같다.
계속계속 반복하기!!
그래도 알고리즘 로직 해결까지는 잘 짜서 뿌듯!!
🌼다른사람 코드🌼
>>같은 로직이지만 문법적으로 이렇게도 쓸수 있구나!
def solution(id_list, report, k):
answer = [0] * len(id_list)
report = set(report)
lst = {}
check = {}
for s in report:
a, b = s.split(' ')
if b not in check:
check[b] = 1
else:
check[b] += 1
if a not in lst:
lst[a] = [b]
else:
if b not in lst[a]:
lst[a] += [b]
for id_, n in check.items():
if n >= k:
for user, user2 in lst.items():
if id_ in user2:
answer[id_list.index(user)] += 1
return answer
'알고리즘 (JAVA) > 프로그래머스 알고리즘' 카테고리의 다른 글
[코테준비 - python] 2016년 level1 - 개발자 배찌 (0) | 2022.12.06 |
---|---|
[코테준비 - python] 성격 유형 검사하기 level1 - 개발자 배찌 (0) | 2022.12.06 |
[코테준비 - python] 키패드 누르기 level1 - 개발자 배찌 (0) | 2022.12.03 |
[코테준비 - python] 이상한 문자 만들기 level1 - 개발자 배찌 (0) | 2022.12.03 |
[코테준비 - python] 실패율 lever1 - 개발자 배찌 (0) | 2022.12.02 |