본문 바로가기

PS/릿코드

[릿코드 1599] Maximum Profit of Operating a Centennial Wheel (파이썬/python)

728x90

https://leetcode.com/problems/maximum-profit-of-operating-a-centennial-wheel/description/

 

Maximum Profit of Operating a Centennial Wheel - LeetCode

Maximum Profit of Operating a Centennial Wheel - You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost d

leetcode.com

 

그리디 문제라고 판단했습니다.

최대 4명을 태울 수 있는 곤돌라에 그리디하게 사람을 최대한 많이 태우고

그때마다의 수익을 판단하여 최대의 수익일때의 회전수를 구해줬습니다.

 

첫번째로 구현한 코드

from typing import List


class Solution:
    def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int:
        max_profit = 0
        rotations = 0

        total_customer = 0
        total_rotations = 0
        wait = 0

        for customer in customers:
            play = 0

            if customer > 4:
                wait += customer - 4
                play = 4

            elif customer < 4:
                play = customer
                if wait != 0:
                    play += wait

                    if play > 4:
                        wait -= (4 - customer)
                        play = 4
                    else:
                        wait = 0

            else:
                play = 4
            # 곤돌라 탑승
            total_customer += play
            total_rotations += 1

            if max_profit < ((total_customer * boardingCost) - (total_rotations * runningCost)):
                max_profit = ((total_customer * boardingCost) - (total_rotations * runningCost))
                rotations = total_rotations

        # 대기 손님이 있을 경우 계속 곤돌라 탑승
        while wait > 0:
            play = 0
            if wait >= 4:
                play = 4
                wait -= 4
            else:
                play = wait
                wait = 0
            # 곤돌라 탑승
            total_customer += play
            total_rotations += 1

            if max_profit < ((total_customer * boardingCost) - (total_rotations * runningCost)):
                max_profit = ((total_customer * boardingCost) - (total_rotations * runningCost))
                rotations = total_rotations

        if max_profit == 0:
            return -1

        return rotations

 

 

추가공부로 수정해본 코드

from typing import List


class Solution:
    def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int:
        max_profit = 0
        rotations = -1

        current_profit = 0
        wait = 0

        rotates = 0

        while wait or rotates < len(customers):
            # 일단 wait 목록에 넣어준다.
            if rotates < len(customers):
                wait += customers[rotates]
            rotates += 1
            # 이미 고객을 wait 에 넣었기 때문에 wait 인수와 4 중에 최소값 만이 현재 탈 수 있는 최대의 고객수 이다.
            boarding = min(wait, 4)
            wait -= boarding

            current_profit += boarding * boardingCost - runningCost

            if max_profit < current_profit:
                max_profit = current_profit
                rotations = rotates

        return rotations
728x90