Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- test-helper
- ssd
- 코드수행
- 전산기초
- 3단계
- 모두를 위한 딥러닝 강좌 시즌1
- 백준
- 1단계
- Python
- 머신러닝
- 딥러닝
- AWS
- cs
- C++
- 이것이 코딩테스트다 with 파이썬
- 자료구조 및 실습
- CS231n
- Object detection
- 실전알고리즘
- 프로그래머스
- docker
- STL
- ubuntu
- 파이썬
- MySQL
- pytorch
- 구현
- SWEA
- 2단계
- 그리디
Archives
- Today
- Total
곰퓨타의 SW 이야기
[프로그래머스 level3 불량 사용자] dictionary와 dfs 활용하기 본문
해결해야하는 문제는 다음과 같았다.
https://programmers.co.kr/learn/courses/30/lessons/64064
코딩테스트 연습 - 불량 사용자
개발팀 내에서 이벤트 개발을 담당하고 있는 "무지"는 최근 진행된 카카오이모티콘 이벤트에 비정상적인 방법으로 당첨을 시도한 응모자들을 발견하였습니다. 이런 응모자들을 따로 모아 불량
programmers.co.kr
이러한 아이디어를 바탕으로 작성한 코드는 다음과 같다.
answer = []
def dfs(ban_dict, ban_ids, idx, id_set):
if len(ban_ids) == idx :
if set(id_set) not in answer:
answer.append(set(id_set))
return
now = ban_ids[idx]
for i in range(len(ban_dict[ban_ids[idx]])) :
if ban_dict[now][i] in id_set :
continue
id_set.append(ban_dict[now][i])
dfs(ban_dict,ban_ids, idx+1, id_set)
id_set.pop()
def solution(user_id, banned_id):
banned_dict = {}
for b in banned_id :
banned_dict[b] = []
for bi in banned_id :
for ui in user_id :
if len(bi) != len(ui):
continue
for idx in range(len(bi)) :
if bi[idx] == '*':
continue
if bi[idx] != ui[idx] :
break
else :
if ui not in banned_dict[bi]:
banned_dict[bi].append(ui)
dfs(banned_dict,banned_id,0,[])
return len(answer)
메모리가 굉장히 많은 경우, 순열을 통해 모든 경우의 수를 조합하고 답이 될 수 있는 쌍을 찾아내는 방식도 가능하다! 하지만 이는 메모리 제한이 있는 경우, 모든 순열 경우의 수를 따지기는 힘들기 때문에 메모리 측면에서는 효율적인 것 같지는 않다. 하지만 이 방식의 경우 코드가 짧고 가독성이 좋아서 가져와보았다.
from itertools import permutations
def check_id(combo_ids,banned_ids):
for i in range(len(combo_ids)):
if len(combo_ids[i]) != len(banned_ids[i]) :
return False
for j in range(len(combo_ids[i])):
if banned_ids[i][j] == '*':
continue
if combo_ids[i][j] != banned_ids[i][j] :
return False
return True
def solution(user_id, banned_id):
answer = []
for ui in permutations(user_id,len(banned_id)):
if check_id(ui,banned_id):
if set(ui) not in answer :
answer.append(set(ui))
return len(answer)
'TIL > 프로그래머스' 카테고리의 다른 글
[프로그래머스 level3 길 찾기 게임] 이진 트리 및 전위순회, 후위순회 구현하기! (0) | 2021.09.08 |
---|---|
[프로그래머스 level3 합승 택시 요금] 최단 경로 구하기(ft. 다익스트라, 플로이드 워셜) (0) | 2021.09.07 |
[프로그래머스 level3 여행경로] bfs/dfs 활용하기 (0) | 2021.09.03 |
[프로그래머스 level3 하노이의 탑] 재귀함수 이용하기! (0) | 2021.08.31 |
[프로그래머스 level3 베스트 앨범] 딕셔너리와 람다 활용하기! (0) | 2021.08.30 |
Comments