Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mingxoxo] programmers_기능개발_Python #18

Merged
merged 1 commit into from
Jan 30, 2025

Conversation

mingxoxo
Copy link
Member

🔗 문제 링크

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

  • 각 배포마다 몇 개의 기능이 배포되는지를 return 하도록 solution 함수를 작성
    • 각 기능의 개발속도는 모두 다르기 때문에 뒤에 있는 기능이 앞에 있는 기능보다 먼저 개발될 수 있고, 이때 뒤에 있는 기능은 앞에 있는 기능이 배포될 때 함께 배포됨

💡 풀이 아이디어

이 문제는 progresses 들이 큐 처럼 선입선출로 배포되어야 한다는 규칙이 있습니다.
따라서 가장 먼저 배포되는 작업이 걸리는 작업 일자가 필요하고, 이후 순차적으로 각 작업이 가장 먼저 배포되는 작업의 작업 일자보다 작거나 같으면 함께 배포됩니다.

실제로 progresses를 큐처럼 관리하여 맨 처음 작업을 배열에서 제거할 수도 있겠지만 시간이 오래 걸리는 작업일 것 같아 하지 않았고 배열을 탐색하면서 처리해주었습니다.

처음에 작업 일자를 구할 때에는 아래와 같이 작성했었는데, 테스트 케이스 중 2번, 11번이 실패하였습니다.
0.5를 더해준 이유는 올림 연산을 하기 위해서 사용하였습니다.

# 실패
processing_day = int((100 - progress) / speed + 0.5)

실패한 이유가 실수 연산에 대한 오차때문인 것 같아 코드를 수정해주니 모든 테스트 케이스를 통과하였습니다.
divmod(a, b)(a // b, a % b) 을 튜플로 반환하는 함수로 가독성이 좋아 편해서 사용하는데 확인해보니 작은 숫자를 다룰 때는 a//b, a%b 와 같이 풀어서 쓰는게 성능 면에서 더 좋다고 합니다.

# 성공
div, mod = divmod(100 - progress, speed)
processing_day = div + int(mod != 0)

📝 새로 학습한 내용

작업 일자를 계산할 때 올림 연산을 하지 않기 위해 음수로 몫을 구하는 방식이 아주 인상깊었습니다..! 🫢

다른 사람의 풀이

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]

📚 참고 자료

@mingxoxo mingxoxo added 🪄Programmers 프로그래머스 문제 풀이 🫧Python 풀이 언어 (Python) labels Jan 19, 2025
@mingxoxo mingxoxo self-assigned this Jan 19, 2025
Copy link
Collaborator

@sangkyu39 sangkyu39 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

배열을 한번만 돌면서 이전 작업과 작업일 수만 비교후 답을 출력해서 간단하고 좋은 것 같아요!

Copy link
Contributor

@just-stopyoon just-stopyoon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

파이썬은 확실히 answer[-1] 을 통해 배열의 길이와 상관없이 마지막 원소에 접근할 수 있다는 점이 편리한 것 같아요!! (갓이썬!!!)
divmod 연산을 활용한 방식이 인상 깊습니다 :) 수고하셨어용 !! 'ㅅ'

@mingxoxo mingxoxo merged commit 1fb8832 into sejongbinary:main Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🪄Programmers 프로그래머스 문제 풀이 🫧Python 풀이 언어 (Python)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants