[프로그래머스/Python] 스택/큐

2023. 9. 5. 01:37·Algorithm/Programmers
import math

def solution(progresses, speeds):
    answer = []
    works = []     # 작업 시간 배열
    for i in range(len(progresses)):
        work = math.ceil((100 - progresses[i]) / speeds[i])
        works.append(work)
    
    big = works[0] # 가장 긴 작업 시간
    cnt = 1
    for i in range(1, len(progresses)):
        if works[i] <= big:
            cnt += 1
        else:
            answer.append(cnt)
            big = works[i]
            cnt = 1
    answer.append(cnt) # 마지막 배포
    return answer

자꾸 테스트 케이스 몇 개가 틀려서 반례를 찾았다.

works = [5, 3, 4] 일 경우 답은 [3] 인데, 예전 내 코드는 바로 전 일수랑만 비교해서 [1, 2] 라는 답이 나왔던 것이다!

 

big은 가장 긴 작업 시간이고, 그 이하의 숫자는 얼마든지 커버(?)할 수 있다!!!

고로 big보다 짧은 작업들은 다 big과 함께 배포되고(if문), big보다 큰 숫자가 나타다면 비로소 새 배포를 시작하게 되는 것이다(else문).

 

그리고 마지막 배포도 잊지말고 append 해주자!

 

 

+) 다른 사람의 풀이

from math import ceil

def solution(progresses, speeds):
    daysLeft = list(map(lambda x: (ceil((100 - progresses[x]) / speeds[x])), range(len(progresses))))
    count = 1
    retList = []

    for i in range(len(daysLeft)):
        try:
            if daysLeft[i] < daysLeft[i + 1]:
                retList.append(count)
                count = 1
            else:
                daysLeft[i + 1] = daysLeft[i]
                count += 1
        except IndexError:
            retList.append(count)

    return retList

try, catch 로 처리한게 참신했다.

big을 설정하지 않고, 가장 큰 값으로 계속 다음 수를 덮어씌웠다.

그러면 간결하게 위 코드처럼 비교가 가능하다.

 

def solution(progresses, speeds):
    Q=[]
    for p, s in zip(progresses, speeds):
        if len(Q)==0 or Q[-1][0]<-((p-100)//s):
            Q.append([-((p-100)//s),1])
        else:
            Q[-1][1]+=1
    return [q[1] for q in Q]

zip이 있다는걸 잊고 있었다!

작업 일수를 계산할 때, 음수(-)로 몫을 구한 다음 다시 양수로 바꿔줬는데 `math.ceil()` 한 것과 동일하다! (내림한 음수, 음수에서 내림은 절댓값 커짐)

Q에 넣어주는 것은 작업 일수와 count(배포 가능한 기능 수)

뒷 작업은 앞 작업의 작업 일수와 비교해서 작으면 앞 작업 count에 +1, 크면 새로 원소를 추가한다

count 부분만 잘라서 답 리턴

'Algorithm > Programmers' 카테고리의 다른 글

[프로그래머스/Python] 베스트앨범  (0) 2023.09.13
[프로그래머스/Python] 의상  (0) 2023.09.12
[프로그래머스/Python] 전화번호 목록  (0) 2023.09.12
[프로그래머스/Python] 뒤에 있는 큰 수 찾기  (0) 2023.09.05
[프로그래머스/Python] 짝지어 제거하기  (0) 2023.09.05
'Algorithm/Programmers' 카테고리의 다른 글
  • [프로그래머스/Python] 의상
  • [프로그래머스/Python] 전화번호 목록
  • [프로그래머스/Python] 뒤에 있는 큰 수 찾기
  • [프로그래머스/Python] 짝지어 제거하기
빵빵0
빵빵0
(아직은) 공부하고 정리하는 블로그입니다.
  • 빵빵0
    Hack Your World
    빵빵0
  • 전체
    오늘
    어제
    • 분류 전체보기 (92)
      • Error Handling (7)
      • Project (5)
        • MEV (2)
      • Architecture (0)
        • API (0)
        • Cache (0)
        • 사소한 고민거리 (0)
      • Computer Science (4)
        • Data Structure (2)
        • Database (1)
        • Cloud (0)
        • OS (0)
        • Infra, Network (1)
        • AI (0)
      • Language (8)
        • Go (8)
        • Rust (0)
        • Python (0)
        • Java (0)
      • Algorithm (40)
        • BaekJoon (18)
        • Programmers (7)
        • LeetCode (6)
        • NeetCode (9)
      • SW Books (9)
        • gRPC Up & Running (1)
        • System Design Interview (2)
        • 스프링 입문을 위한 자바 객체지향의 원리와 이해 (6)
        • 블록체인 해설서 (0)
        • 후니의 쉽게 쓴 CISCO 네트워킹 (0)
      • BlockChain (4)
        • Issues (0)
        • Research (4)
        • Tech (0)
      • Own (8)
        • TIR(Today I Read) (3)
        • Personal (2)
        • Novel (0)
        • Memo (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    프로그래머스
    Hash Table
    ethereum
    큐
    Palindrome
    candlechart
    2024
    golang
    BFS
    BEAKJOON
    chart
    KBW
    blockchain
    블록체인
    goroutine
    BaekJoon
    MEV
    NeetCode
    context
    go
    Greedy
    MongoDB
    Programmers
    스택
    백준
    LeetCode
    DP
    EVM
    Python
    two pointer
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
빵빵0
[프로그래머스/Python] 스택/큐
상단으로

티스토리툴바