Programmers / 3단계 / 두 큐 합 같게 만들기 / python / 2022 KAKAO TECH INTERNSHIP

2024. 3. 31. 16:44·코딩테스트/programmers (python)

 

https://school.programmers.co.kr/learn/courses/30/lessons/118667

 

 

나의 풀이

from collections import deque 
def solution(queue1, queue2):
    q1 = deque(queue1)
    q2 = deque(queue2)
    
    s1 = sum(q1)
    s2 = sum(q2) 
    sumnum = s1 + s2
    
    if sumnum % 2 == 1:
        return -1
    
    cnt = 0
    while True:
        
        if s1 > s2 and len(q1) != 0:
            num = q1.popleft()
            q2.append(num)
            s1 -= num
            s2 += num
            cnt += 1 
        
        elif s2 > s1 and len(q2) != 0:
            num = q2.popleft()
            q1.append(num)
            s2 -= num
            s1 += num
            cnt += 1
        
        else:
            return cnt

 

 

테스트 3 시간 초과 - while 문 위에서 두 큐의 합이 홀수일 때 -1을 리턴하도록 처리하였으나, 두 큐의 합이 짝수임에도 똑같이 나눌 수 없는 경우를 처리하는 부분을 구현하지 못하였다.

 

모범 답안

from collections import deque 
def solution(queue1, queue2):
    q1 = deque(queue1)
    q2 = deque(queue2)
    
    limit = len(q1) * 4
    s1 = sum(q1)
    s2 = sum(q2) 
    
    sumnum = s1 + s2
    
    if sumnum % 2 == 1:
        return -1
    
    cnt = 0
    while True:
        
        if s1 > s2 and len(q1) != 0:
            num = q1.popleft()
            q2.append(num)
            s1 -= num
            s2 += num
            cnt += 1 
        
        elif s2 > s1 and len(q2) != 0:
            num = q2.popleft()
            q1.append(num)
            s2 -= num
            s1 += num
            cnt += 1
        
        else:
            break
            
        if cnt > limit:
            return -1
        
    return cnt

 반복문 제한을 두기 위해 limit로 조건을 걸었다. queue1과 queue2의 모든 원소를 바꾸면 queue1 길이의 2배만큼 횟수가 필요하고 다시 모든 원소를 바꿔 원래의 모습으로 만들면 queue1 길이의 2배만큼 필요해 총 len(q1) * 4 만큼 횟수가 필요하기 때문에 limit를 len(q1)로 두었다.

 

참조

 

https://a-littlecoding.tistory.com/123

 

[프로그래머스] 두 큐 합 같게 만들기_Python level2(스택/큐)

🔒 문제 https://school.programmers.co.kr/learn/courses/30/lessons/118667 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합

a-littlecoding.tistory.com

 

'코딩테스트 > programmers (python)' 카테고리의 다른 글

Programmers / 2단계 / 큰 수 만들기 / python / 탐욕법(Greedy)  (0) 2024.04.04
Programmers / 2단계 / 삼각 달팽이 / python  (0) 2024.04.02
Programmers / 3단계 / 단속카메라 / python / 탐욕법(Greedy)  (0) 2024.03.29
Programmers / 3단계 / 등굣길 / python / 동적계획법(Dynamic Programming)  (0) 2024.03.28
Programmers / 2단계 / 소수 찾기 / python / 완전 탐색  (0) 2024.03.28
'코딩테스트/programmers (python)' 카테고리의 다른 글
  • Programmers / 2단계 / 큰 수 만들기 / python / 탐욕법(Greedy)
  • Programmers / 2단계 / 삼각 달팽이 / python
  • Programmers / 3단계 / 단속카메라 / python / 탐욕법(Greedy)
  • Programmers / 3단계 / 등굣길 / python / 동적계획법(Dynamic Programming)
seulll
seulll
개인 공부 / 정리 블로그입니다
  • seulll
    seulll
    seulll
  • 전체
    오늘
    어제
    • 분류 전체보기 (320) N
      • 코딩테스트 (221) N
        • programmers (python) (155)
        • 백준 (python) (64) N
      • 자료구조 | 알고리즘 (14)
      • 개발 | 프로젝트 (18) N
        • Python (4)
        • Java | Spring (6)
        • Unity (3)
        • API (3)
      • CS (15)
        • Network (5)
        • SQL (2)
        • OS (4)
      • 데이터 분석 (14)
      • 기타 (12)
  • 블로그 메뉴

    • 홈
    • 태그
    • 글쓰기
    • 설정
  • 링크

    • GitHub
  • 인기 글

  • 태그

    박스플롯
    프로그래머스
    티스토리챌린지
    solving environment
    모델 성능 평가
    API
    Boxplot
    카카오맵 api
    confusion matrix
    백엔드 개발자 역량
    2 x n 타일링
    야근 지수
    웹크롤링
    train_test_split
    코딩테스트
    Python
    오차행렬
    kakao map api
    바다코끼리
    데이터분석
    asterisk
    카카오맵
    오블완
    파이썬
    Greedy
    대입 표현식
    백엔드
    백엔드 개발자
    프렌즈4블록
    그리디 알고리즘
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
seulll
Programmers / 3단계 / 두 큐 합 같게 만들기 / python / 2022 KAKAO TECH INTERNSHIP
상단으로

티스토리툴바