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 |
Tags
- DP
- level3
- 프로그래머스
- 브루트포스
- 운영체제
- 백준
- 그리디
- BOJ
- dict
- 재귀
- level0
- dfs
- python
- level1
- 다이나믹 프로그래밍
- level2
- 스택
- 힙
- 수학
- 다익스트라
- BFS
- 가상메모리 관리
- programmers
- 구현
- N과M
- 딕셔너리
- 파이썬
- 코딩테스트
- MYSQL
- 가상메모리
Archives
- Today
- Total
동캄의 코딩도장
프로그래머스 level2 [시소 짝꿍] 파이썬 본문
https://school.programmers.co.kr/learn/courses/30/lessons/152996
#프로그래머스 level2 시소 짝꿍
from collections import defaultdict
def solution(weights):
answer = 0
length=len(weights)
dic=defaultdict(int)
for weight in weights:
dic[weight]+=1
for weight in dic.keys():
if dic[weight]>1:
answer+= dic[weight]*(dic[weight]-1)//2
if weight *2 in dic.keys():
answer+= dic[weight] *dic[weight*2]
if weight *3/2 in dic.keys():
answer += dic[weight] * dic[weight*3/2]
if weight *4/3 in dic.keys():
answer+=dic[weight]*dic[weight*4/3]
return answer
print(solution([100,180,360,100,270]))
맨 처음에는 O(N^2)으로 해결하려고 하였다. 하지만, 시간 초과로 문제가 해결되지 않았다.
(최대 길이가 10만이므로, 10만*10만 == 100억...)
dictionary를 이용하여 해결하려 하였는데, 같은 수가 있는 경우를 어떻게 처리해야할지 난감했다. 한참을 고민해도 떠오르지 않아 다른 분의 풀이를 참고하여 해결하였다.
'코테 > 프로그래머스' 카테고리의 다른 글
프로그래머스 level2 [디펜스 게임] 파이썬 (0) | 2023.04.16 |
---|---|
프로그래머스 level2 [연속된 부분 수열의 합] 파이썬 (0) | 2023.04.14 |
프로그래머스 level2 [요격 시스템] 파이썬 (0) | 2023.04.14 |
프로그래머스 level2 [마법의 엘리베이터] 파이썬 (0) | 2023.04.13 |
프로그래머스 level2 [무인도 여행] 파이썬 (0) | 2023.04.13 |