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
- 오픽
- 우선순위큐
- 구현
- 슬라이싱
- 재귀
- 백준
- 오픽독학
- %H
- 약수구하기
- set
- heapq
- 완전이진트리
- issubset
- 최대재귀높이
- 삼중for문 탈출
- not null
- 이진탐색
- dfs
- 파일명 변경
- 복사
- 딕셔너리
- Deque
- 브루트포스
- date_format
- 제곱근
- IH
- bfs
- 스택
- Inner Join
- 파이썬
Archives
- Today
- Total
나예
[프로그래머스] 시소짝꿍 본문
1. 문제
https://school.programmers.co.kr/learn/courses/30/lessons/152996
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
2.시간초과
from itertools import combinations
def solution(weights):
answer = 0
for twin in combinations(weights,2):
if twin[0] == twin[1]:
answer +=1
continue
if twin[0] < twin[1]:
if (twin[0] * 3 //2 == twin[1] ) or (twin[0] * 2 == twin[1] ) or (twin[0] * 4 //3 == twin[1] ):
answer +=1
else:
if (twin[1] * 3 //2 == twin[0] ) or (twin[1] * 2 == twin[0] ) or (twin[1] * 4 //3 == twin[0] ):
answer +=1
return answer
시간 초과 combitions 쓰면 100000 **2라 시간초과 남
def solution(weights):
answer = 0
dict= {}
for w in weights:
if w not in dict.keys():
dict[w] =1
else:
dict[w] +=1
print(dict)
for weight in dict.keys():
if dict[weight] >=2:
answer += (dict[weight] * (dict[weight]-1) ) /2
if weight *2 in dict.keys():
answer += dict[weight] * dict[weight *2]
if weight *2 /3 in dict.keys():
answer += dict[weight] * dict[weight *2 /3]
if weight *4 /3 in dict.keys():
answer += dict[weight] * dict[weight *4 /3]
return answer
딕셔너리에 몸무게 별 개수를 저장해서 풀었다
728x90