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
- 구현
- N과M
- BFS
- 다이나믹 프로그래밍
- level0
- 그리디
- 백준
- dfs
- 스택
- 프로그래머스
- 코딩테스트
- 운영체제
- programmers
- 딕셔너리
- BOJ
- 다익스트라
- MYSQL
- 재귀
- dict
- level3
- DP
- 가상메모리
- 힙
- python
- level2
- 브루트포스
- 파이썬
- 수학
- 가상메모리 관리
- level1
Archives
- Today
- Total
동캄의 코딩도장
프로그래머스 level2 [시소 짝꿍] 파이썬 본문
https://school.programmers.co.kr/learn/courses/30/lessons/152996
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#프로그래머스 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 |